[英]How to Load html file in to WebView from Device storage downloads folder
[英]cordova load image file from device storage
我有一个图像标签,它是对应用程序目录中图像文件的 src 引用。 我使用cordova.file.applicationStorageDirectory
来获取应用程序目录并将其余路径添加到它(不是:我已经使用java android studio在其中创建了新目录和图像)。 所以我的 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 标签是空的,所以这似乎不是一个正确的方法。 如何将应用程序目录中的图像加载到图像标签。
您可以使用“cdvfile://”协议直接放置图像。 将其用作:
<img src="cdvfile://localhost/persistent/img/logo.png" />
更多细节在这里: https : //github.com/apache/cordova-plugin-file#cdvfile-protocol
我知道这个问题是针对 Android 上的 Cordova,但供将来参考 - 这在 iOS 上不起作用,因为最新的 WKWebView 有一个 CORS 问题。 您将需要手动读取文件并注入图像。 像这样的东西:
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.