问题描述
我正在使用the cordova-plugin-camera
用navigator.camera.getPicture
从相册中获取图片。
到destinationType: destinationType.FILE_URI.
这会给我返回一个File_URI = file:///storage/emulated/0/Android/data/{app}/cache/IMG20200916194914.jpg?1600372401486
,其路径指向保存图像的缓存文件夹。
我需要将该图像作为div的背景放置以用作墙纸,例如:
$("#imageCustom").css("background-image","url(" + imageURI + ")");
问题是它引发了一个错误,提示:
Not allowed to load local: resource:file:///storage/emulated/0/Android/data/{app}/cache/IMG20200916194914.jpg?1600372401486
有没有一种方法可以获取到该文件夹/文件的相对路径,以供html访问?
或者可能是将图像移到另一个文件夹,我该怎么做? (我用resolveLocalFileSystemURL/requestFileSystem/copyTo/LocalFileSystem.PERSISTENT
复制了它,但唯一要做的就是将它复制到我遇到相同问题的files
文件夹中。
谢谢。
解决方法
感谢JM-AGMS
cordova load image file from device storage中的发布使用了cordova-plugin-file
中包含的cdvfile protocol
。
1。将cdvfile:方案添加到索引页面的Content-Security-Policy元标记中,例如:
<meta http-equiv="Content-Security-Policy" content="default-src 'self' data: gap: cdvfile: https://ssl.gstatic.com 'unsafe-eval'; style-src 'self' 'unsafe-inline'; media-src *">
2。将<access origin="cdvfile://*" />
添加到config.xml
。
3。并用于将路径转换为图像:
resolveLocalFileSystemURL(imageURI,function(entry) {
console.log('cdvfile URI: ' + entry.toInternalURL());
$("#imageCustom").css("background-image","url(" + entry.toInternalURL() + ")");
$("#imageCustom").css("display","block");
});
resolveLocalFileSystemURL(imageURI);