PhoneGap的生成:在应用程序中的某个文件夹中下载图片应用程序、下载图片、文件、夹中

2023-09-06 06:08:06 作者:人走茶会凉

我能够下载图像,如果我直接指定路径与

I am able to download an image if I specify the path directly with

文件:///存储/ sdcard0 /

file:///storage/sdcard0/

我如何将图像保存到我在我的应用程序文件夹中的一个?我尝试this方法来设置应用程序的路径,但它不为我工作。

How can I save an image to my one of the folders in my app ? I tried this approach to set app path but it doesn't work for me.

这是我使用的是什么,到目前为止,它的工作原理,如果你想保存和图像SD卡:

This is what I am using so far and it works if you want to save and image to sdcard:

var fileName = "myImage.png";  
var fileTransfer = new FileTransfer();

var uri = encodeURI("https://m.xsw88.com/allimgs/daicuo/20230906/1292.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 /。我该如何获得正确的路径,以我的应用程序?

I get /android_asset/www/. How would I get the correct path to my app?

该应用程序使用AngularJS并没有自举在onDeviceReady(开发之前我做这样的说法,现在改变为类似this我是无法理解(试过,但没有工作))。

The app is using AngularJS and is not bootstrapped in onDeviceReady (developer before me made it that way and now changing it to something like this is beyond me (tried but didn't work)).

另一个问题可以参考我被问here. 我也试过这个,this, 这和this但没有为我工作。我得到的完整路径,最近我设法路径印有.toURL(),它是cdvfile://本地主机/老大难。 上面下载code现在也适用,如果我用

Another question I can refer to was asked here. I also tried this, this, this and this but none worked for me. I get "" for fullPath and recently I managed to get path printed with .toURL() and it was "cdvfile://localhost/persistent". The above download code now also works if I use

filePath = "cdvfile://localhost/persistent/MyAppID/uploads/myImage.png";

但是这创造了文件夹/ MyAppID /上传中/存储/ sdcard0这是坏了。我的应用程序需要去图像

but this creates folder /MyAppID/uploads in /storage/sdcard0 which is bad again. My app needs to get to images with

<img src="uploads/myImage.png" alt="myimg"/>

另一个有用的链接这里但它没有提供任何帮助,如何写你自己的应用程序pre-创建的文件夹。

Another useful link is here but it offers no help as to how to write to pre-created folder of your own app.

编辑:据我已经学会了,你不能在自己的应用程序写(只读)。这就是为什么我想引用从SD卡图像

As far as I've learned you cannot write within your own app(read only). That's why I tried to reference images from sdcard with

<img src="file:///storage/sdcard0/Android/data/com.app.my/cache/myImage.png" alt="myimg"/>

和这个作品! :)不幸的是,这是很难codeD及自其他手机可能有不同的持久位置并不好。 如果我尝试

and this works! :) Unfortunately this is hardcoded and not good since other phones might have a different persistent location. If I try

<img src="cdvfile://localhost/persistent/Android/data/com.app.my/cache/myImage.png" alt="myimg"/>

这不引用图片:/(它是下载的存储/ sdcard0 /安卓/数据/ com.app.my /缓存/虽然)

this does not reference the picture :/ (it does download it on storage/sdcard0/Android/data/com.app.my/cache/ though)

推荐答案

据我了解到你不能写自己的应用程序(您的应用程序内部变化项目 - Android电子/网络文件夹)。

As far as I learned you cannot write to your own app (change items inside your app - /www folder on Android).

我的解决办法是将图像保存到永久存储(SD卡通常情况下)。 你可以让你的应用程序的持久性存储的路径通过以下步骤:

My solution was to save images to PERSISTENT storage (sdcard usually). You can get the path of your app's persistent storage with these steps:

function onDeviceReady() {
    console.log('Device Platform: ' + device.platform);  // returns 'Android' or 'iOS' for instance
    window.requestFileSystem(LocalFileSystem.PERSISTENT, 0, gotFS, fail); 
    cacheFolderSubPath = "Android/data/com.id.myApp/cache/"; // this is app's cache folder that is removed when you delete your app! (I don't know what this path should be for iOS devices)  
}

然后在里面gotFS成功回调

Then inside gotFS success callback

function gotFS(fileSystem) { 
    var nturl = fileSystem.root.toNativeURL(); // cdvfile://localhost/persistent/
    window.resolveLocalFileSystemURL(nturl+cacheFolderSubPath, onResolveSuccess, onResolveFail);
}

和在onResolveSuccess

and in onResolveSuccess

function onResolveSuccess(fileEntry){
    appCachePath = fileEntry.toNativeURL()+"/"; // file:///storage/sdcard0/Android/data/com.id.myApp/cache/ in my case but some other Androids have storage/emulated/0 or something like that ... 
    continueCustomExecution();
}

和现在continueCustomExecution(),您可以运行您的程序,做什么是你做的......在这种情况下下载图像到appCachePath我们先前得到的。您现在可以成功引用SRC标记的图像与我们appCachePath + yourImageName。

and now in continueCustomExecution() you can run your program and do whatever it is you do ... and in this case download images into appCachePath that we got earlier. You can now successfully reference images in src tags with our appCachePath+yourImageName.

不幸的是我仍然不知道如何成功下载一个图像上的的iOS 。我得到一个错误code 1与文件传输插件...(保存到文件:///var/mobile/Applications/my.app.id/Documents/)。也许应该保存在其他地方,但是这是我要求固定文件系统时,你得到的路径。

Unfortunately I still don't know how to download an image successfully on iOS. I get an error code 1 with FileTransfer plugin ... (saving to file:///var/mobile/Applications/my.app.id/Documents/). Probably should save somewhere else but this is the path I get when requesting PERSISTENT FileSystem.

干杯