如果我直接指定路径,我就可以下载图像
文件:///存储/sdcard0/
如何将图像保存到应用程序中的文件夹之一?我试过this设置应用程序路径的方法,但它对我不起作用。
这是我到目前为止所使用的,如果您想将图像保存到 SD 卡,它可以工作:
var fileName = "myImage.png";
var fileTransfer = new FileTransfer();
var uri = encodeURI("http://my.url.to.image/myImage.png");
var filePath = "file:///storage/sdcard0/uploads/myImage.png";
fileTransfer.download(
uri,
filePath,
function(entry) {
alert("download complete: " + entry.fullPath);
console.log("download complete: " + entry.fullPath);
},
function(error) {
alert("download error source/target/code:\n" + error.source +" \n||| "+error.target+" \n||| "+error.code);
console.log("download error source/target/code " + error.source+" / "+error.target+" / "+error.code);
}
);
如果我使用这个功能:
function getPhoneGapPath() {
'use strict';
var path = window.location.pathname;
var phoneGapPath = path.substring(0, path.lastIndexOf('/') + 1);
return phoneGapPath;
}
我得到/android_asset/www/。我如何获得应用程序的正确路径?
该应用程序使用 AngularJS,并且没有在 onDeviceReady 中引导(在我之前的开发人员就是这样做的,现在将其更改为类似的内容)this超出了我的范围(尝试过但没有成功))。
另一个我可以参考的问题被问到here.
我也尝试过this, this, this and this但没有一个对我有用。我得到 fullPath 的“”,最近我设法用 .toURL() 打印路径,它是“cdvfile://localhost/persistent”。如果我使用的话,上面的下载代码现在也可以工作
filePath = "cdvfile://localhost/persistent/MyAppID/uploads/myImage.png";
但这会在 /storage/sdcard0 中创建文件夹 /MyAppID/uploads ,这又很糟糕。我的应用程序需要获取图像
<img src="uploads/myImage.png" alt="myimg"/>
另一个有用的链接是here但它对于如何写入您自己的应用程序的预先创建的文件夹没有提供任何帮助。
EDIT:据我所知,您无法在自己的应用程序中编写(只读)。这就是为什么我尝试使用 sdcard 来引用图像
<img src="file:///storage/sdcard0/Android/data/com.app.my/cache/myImage.png" alt="myimg"/>
这有效! :) 不幸的是,这是硬编码的并且不好,因为其他手机可能有不同的持久位置。
如果我尝试
<img src="cdvfile://localhost/persistent/Android/data/com.app.my/cache/myImage.png" alt="myimg"/>
这并不引用图片:/(但它确实将其下载到 storage/sdcard0/Android/data/com.app.my/cache/ 上)