一些背景:
Platform: Android 4.4.2可访问整个内核源代码 . 编译我自己的Android版本
App in question: 它严格来说是一个webview / HTML5应用程序 . Javascript / jQuery中的所有内容 . Android活动只是启动webview然后Javascript完成剩下的工作 .
Function: 当你启动应用程序时,平台自动在后台通过wifi / BT等接收jpeg.HW / OS在Android文件系统中本地保存图像说/data/somefolder/image.jpeg
现在webview读取此图像并显示(它可以)并且还需要存储在IndexedDB中以进行缓存 . 这就是问题所在 .
根据以下代码使 XMLHttpRequest()
加载图像时:
var db = Database.indexedDB.tags;
// Create XHR
var xhr = new XMLHttpRequest(), blob;
xhr.open("GET", "file:///data/somefolder/image.jpeg", true); //I suspect I am not specifying the image correctly
// Set the responseType to blob
xhr.responseType = "blob";
xhr.addEventListener("load", function () {
if (xhr.status === 200) {
// Blob as response
blob = xhr.response;
console.log("Blob Retrieved from image URL");
var browserName = BrowserType().brand;
// Put the blob into the dabase
if (browserName == 'Chrome') {
// Chrome can't store blobs at the moment.
// Convert to base64.
convertToBase64(blob, continuation);
} else {
continuation(blob);
}
function continuation(blob) {
// Open a transaction to the database
var transaction = db.transaction(["tagStore"], 'readwrite');
tag.blob = blob;
var putRequest = transaction.objectStore("tagStore").put(tag, new Date().getTime());
putRequest.onsuccess = function(event) {
console.log( "blob Stored in database!");
};
putRequest.onerror = function(event) {
console.log(key + " Storage Error:" + event);
};
}
}else{
console.log("ERROR: Blob Can't Be Retrieved from URL:" + "xhr.status = "+xhr.status);
}
}, false);
xhr.onreadystatechange = function(){
//console.log(xhr.readyState+" " + xhr.status);
}
// Send XHR
xhr.send();
Error when above code executes
XMLHttpRequest cannot load file:///data/somefolder/image.jpeg Cross origin requests are only supported for HTTP.
我的indexedDB实现源自一个非常好的教程Mozilla Hacks . 和其他这样的例子 . 我确保它适用于我的其他应用程序 .
Problem Statement: 那么,有没有办法将图像从Android文件系统保存到IndexedDB?
谢谢 .