How to access data saved in app folder [SOLVED]


#1

hi all,

i have some question about file management in ionic. i know in can access the app storage with $cordovafile or with window.requestFileSystem and download resources from the internet, but i cannot access the downloaded resources.
its just a matter of permission?..do i need a routed device?..is possibile to save data OUTSIDE the sandboxed space such as for example the general “download” folder?..

thanks in advance


#2

I ‘download’ and access files from the APK using plugins cordova-plugin-file 3.0.0 “File” and cordova-plugin-file-transfer 1.4.0 “File Transfer”.

As far as I can see you should be able to do the same with internet files by just changing the URL. Is the following code to any use to you?

var path = ‘extra/abcd.txt’;
$ionicPlatform.ready(function() {

if (window.cordova) {
    var url = cordova.file.applicationDirectory + 'www/' + path;
    var targetPath = cordova.file.dataDirectory + path.substring(path.lastIndexOf('/') + 1);
    var options = {
        encodeURI: false
    };
    var trustHosts = true;
    // Get file from APK
    $cordovaFileTransfer.download(url, targetPath, options, trustHosts).then(
        function(result) {
            $log.log("Successfully got file from APK: " + JSON.stringify(result));
            // File saved to data directory, get it.
            window.resolveLocalFileSystemURL(result.nativeURL,
                function (fileEntry) {
                    // Create file object
                    fileEntry.file(function(file) {
                        $log.log("Got local file");
                        var reader = new FileReader();
                        // Callback called when reading file is complete
                        reader.onloadend = function () {
                            $log.log("File size: " + reader.result.byteLength);
                            var fileContent = reader.result;
                        };
                        // Read file into an ArrayBuffer
                        reader.readAsArrayBuffer(file);
                    },
                    function() {
                        $log.log("Failed to get file from data directory");
                    });
                }
            );
        },
        function(error) {
            $log.log("Failed to fetch file from APK: " + JSON.stringify(error));
        }
    );
}

});


#3

Try This

//document.addEventListener(“deviceready”, function () {
//
// if(window.Connection) {
// if(navigator.connection.type == Connection.NONE) {
// //if no wifi connection
// $scope.Openpdf=function()
// {
// window.requestFileSystem(LocalFileSystem.PERSISTENT, 0, function(fs) {
// fs.root.getDirectory(
// “JSA”,//Your directory name
// {
// create: false
// },
// function(dirEntry) {
// dirEntry.getFile(
// $rootScope.JSAID+".pdf", //Save with PDF Name or txt name
// {
// create: false,
// exclusive: false
// },
// function gotFileEntry(fe) {
// $ionicLoading.hide();
// $scope.imgFile = fe.toURL();
// //For open the pdf
// var open = cordova.plugins.disusered.open;
//
// function success() {
// console.log(‘Success’);
// }
//
// function error(code) {
// if (code === 1) {
// console.log(‘No file handler found’);
// } else {
// console.log(‘Undefined error’);
// }
// }
//
// open($scope.imgFile, success, error);
// },
// function(error) {
// $ionicLoading.hide();
// $cordovaToast.showLongCenter("Pdf Not Available for this "+$rootScope.JSAID);
// console.log(“Error getting file”);
// }
// );
// }
// );
// },
// function() {
// $ionicLoading.hide();
// console.log(“Error requesting filesystem”);
// });
// }
// }
// else
// {
// //if wifi connection having
// Save();
//
// }
// }
// },false);


#4

its more or less what i’ve already done, in my specific case i noticed that ive properly downloaded files cannot be opened. i tried two mime types: a png file and an apk file (that is basically the updated apk of my current app),…i also noticed that i cannot access folder or files OUTSIDE the storage space of my app. i’m stucked on this point…

files:///data/data/package_name/files/files/file_name.apk

this url cannot be opened by android and i cannot install apk or open whatever file…

i’ve used file, filetransfer and fileopener2

EDIT :

i got this thing working. i changed the path to “/storage/emulated/0/” :slightly_smiling:


#5

use ‘/sdcard/JSA/’ JSA is folder name