[英]cordova load image file from device storage
I have an image tag that is src references to an image file in application directory.我有一个图像标签,它是对应用程序目录中图像文件的 src 引用。 I used
cordova.file.applicationStorageDirectory
to get the app directory and add the rest of path to it ( not: I already created the new directory and image in it using java android studio).我使用
cordova.file.applicationStorageDirectory
来获取应用程序目录并将其余路径添加到它(不是:我已经使用java android studio在其中创建了新目录和图像)。 so my full path for img src is file:///data/data/com.test.Dir/myfoolder/myimg.jpg
so for test I use it like <img id="the_img" src="file:///data/data/com.test.Dir/myfoolder/myimg.jpg" width="100" height="150">
but the img tag is empty so it seems this is not a right way.所以我的 img src 的完整路径是
file:///data/data/com.test.Dir/myfoolder/myimg.jpg
所以为了测试我使用它像<img id="the_img" src="file:///data/data/com.test.Dir/myfoolder/myimg.jpg" width="100" height="150">
但 img 标签是空的,所以这似乎不是一个正确的方法。 how can I load images in app directory to image tag.如何将应用程序目录中的图像加载到图像标签。
You can use the "cdvfile://" protocol to put images directly.您可以使用“cdvfile://”协议直接放置图像。 Use it as:
将其用作:
<img src="cdvfile://localhost/persistent/img/logo.png" />
More details here: https://github.com/apache/cordova-plugin-file#cdvfile-protocol更多细节在这里: https : //github.com/apache/cordova-plugin-file#cdvfile-protocol
I understand that this question is for Cordova on Android, but for future reference - This does not work on iOS because of the latest WKWebView which has a CORS problem with this.我知道这个问题是针对 Android 上的 Cordova,但供将来参考 - 这在 iOS 上不起作用,因为最新的 WKWebView 有一个 CORS 问题。 You will need to read the file manually and inject the image.
您将需要手动读取文件并注入图像。 Something like this:
像这样的东西:
function loadImageFromFile(filename, index) {
window.resolveLocalFileSystemURL(filename, function success(fileEntry) {
fileEntry.file(function (file) {
var reader = new FileReader();
reader.onloadend = function() {
if (this.result) {
var elem = document.getElementById("imageitem");
var blob = new Blob([new Uint8Array(this.result)], { type: "image/png" });
elem.src = window.URL.createObjectURL(blob);
}
};
reader.readAsArrayBuffer(file);
});
}, function () {
console.log("File not found: ");
});
}
<img id="the_img" src="/storage/emulated/0/Android/data/com.test.Dir/files/myimg.jpg" width="100" height="150">
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.