Phonegap Build: download an image in one of your application folders

I can load the image if I specify the path directly with

File: /// storage / sdcard0 /

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

This is what I have been using so far, and it works if you want to save the image to SDCard too:

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); 
    }  
); 

      

If I use this function:

function getPhoneGapPath() {
    'use strict';
    var path = window.location.pathname;
    var phoneGapPath = path.substring(0, path.lastIndexOf('/') + 1);
    return phoneGapPath;
}

      

I am getting / android_asset / www /. How do I get the correct path for my application?

The app uses AngularJS and doesn't load in onDeviceReady (the developer did it this way before me and now changing it to something like this is outside of me (tried it but doesn't work)).

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

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

      

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

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

      

Another helpful link here , but it doesn't provide any help on how to write to the pre-created folder of your own application.

EDIT: As far as I understood, you cannot write in your (read-only) application. This is why I tried to reference images from SDCard with

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

      

and it works! :) Unfortunately this is hardcoded and not very good as other phones may have a different permanent location. If i try

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

      

this is not the case for the image: / (it loads it into storage / sdcard0 / android / data / com.app.my / cache / though)

+1


source to share


2 answers


As far as I found out, you cannot write to your application (change elements inside your application - / www on Android).

My solution was to save the images to the PERSISTENT storage (usually sdcard). You can get the path to the persistent app store in the following ways:

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 cache folder that is removed when you delete your app! (I don't know what this path should be for iOS devices)  
}

      

Then inside the gotFS callback function

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

      



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();
}

      

and now in continueCustomExecution () you can run your program and do whatever you do ... in which case load the images in appCachePath that we got earlier. You can now successfully link to images in src tags with our appCachePath + yourImageName.

Unfortunately, I still don't know how to upload the image successfully on iOS . I am getting error code 1 with the FileTransfer plugin ... (saving to file: ///var/mobile/Applications/my.app.id/Documents/). Probably needs to be saved somewhere else, but this is the path I get when I request the PERSISTENT FileSystem.

Greetings

+1


source


I would use a folder alias as defined in the plugin docs here ( https://github.com/apache/cordova-plugin-file/blob/master/doc/index.md ). Specifically cordova.file.dataDirectory. Note that this is not an afike under the www of your original project, but seems to be the preferred location for storing downloads. Once saved there, you can resolve it to a URL, which can be used for loading via AJAX, or into an img tag if you're loading graphics.



0


source







All Articles