1

I am trying to display a list of file that are store on the android device in my cordova app and I am using the cordova -file plugin .However, I am not seeing the file when I select the browse button in my app but I see the file in the system "My Files" android app.

Here is the list of folder that I am iterating thru

[cordova.file.externalRootDirectory,cordova.file.dataDirectory]

On the android phone, I see the files bu doing the following:

  1. select /settings/storage/internal storage

  2. selecting explore

  3. On the emnu the heading says MyFiles>device storage

  4. select data or download ..

    On the device cordova.file.externalRootDirectory resolve to file:///storage/emulated/0/download

However, I don't see any files

Here my code

$scope.showLocalFileOnAndroid = function () {
            $scope.showLocalAndroidFiles = true;
            var localURLs = [cordova.file.externalRootDirectory,cordova.file.dataDirectory
                            ];
            var index = 0;
            var i;
            var errorStr = '';
            var fileList = [];
            var addFileEntry = function (entry) {
                var dirReader = entry.createReader();
                dirReader.readEntries(
                    function (entries) {

                        var i;
                        for (i = 0; i < entries.length; i++) {
                            if (entries[i].isDirectory === true) {
                                // Recursive -- call back into this subdirectory
                                addFileEntry(entries[i]);
                            } else {
                                var ext = entries[i].name.split('.').pop();
                                if (ext === 'doc' || ext === 'docx' ||
                                    ext === 'rdf' || ext === 'pdf' || ext === 'txt' ||
                                    ext === 'odt') {

                                    fileList.push(entries[i]); // << replace with something useful
                                }
                                index++;
                            }
                        }
                    },
                    function (error) {
                        console.log('readEntries error: ' + error.code);
                        errorStr += '<p>readEntries error: ' + error.code + '</p>';
                    }
                );
            };
            var addError = function (error) {
                console.log('getDirectory error: ' + error.code);
                errorStr += '<p>getDirectory error: ' + error.code + ', ' + error.message + '</p>';
            };
            for (i = 0; i < localURLs.length; i++) {
                if (localURLs[i] === null || localURLs[i].length === 0) {
                    continue; // skip blank / non-existent paths for this platform
                }
                window.resolveLocalFileSystemURL(localURLs[i], addFileEntry, addError);
            }
            $scope.fileList = fileList;
            $scope.localFileError = errorStr;
        };
Community
  • 1
  • 1
user2570135
  • 2,669
  • 6
  • 50
  • 80
  • possible duplicate of unanswered question here: http://stackoverflow.com/questions/40557762/list-the-files-that-are-stored-in-the-download-folder-on-an-android-device – Matthias Feb 27 '17 at 16:27
  • Thanks .. I saw that answer and I have copied some of the code , However , it does not work .. The files on the device is store in "Internal Storage\root\sdcard\Download" and none of path define in that code make reference this path – user2570135 Feb 27 '17 at 16:45
  • this appears to be a common problem, [yet another unanswered question](http://stackoverflow.com/questions/41152137/cordova-file-download-to-android-download-folder) regarding Cordova and the Download folder. – Matthias Feb 28 '17 at 11:52
  • probably not the issue but you have `cordova.file.dataDirectory` twice in the `localURLs` array. I don't see a reason for that – Matthias Feb 28 '17 at 14:12
  • I remove the duplicate URL . It not going thru the correct folder in the android phone – user2570135 Feb 28 '17 at 15:52
  • maybe this is because of the difference between `file:///storage/emulated/0/download` which I assume is a simulated Download folder created by Cordova and then the typical Android Download folder (which you are accessing on the phone) which is e.g. `sdcard/Download`? – Matthias Feb 28 '17 at 16:04
  • I think so, but how do I access this folder,, I have tried file :///sdcard/Download and I got nothing back from the call – user2570135 Feb 28 '17 at 16:49

1 Answers1

0

Here's something. Maybe you'll have to do something like this. This is probably not as portible between devices as using the cordova plugin though.

window.requestFileSystem(LocalFileSystem.PERSISTENT, 0, onFileSystemSuccess, fail);

var localURLs = [cordova.file.externalRootDirectory,
                 cordova.file.dataDirectory, 
                 "file:///Download"]; // or "file:///sdcard/Download" or "file:///storage/Download" or "file:///storage/download" or something

I used this as reference.

Perhaps all you were missing was the call to requestFileSystem.

Community
  • 1
  • 1
Matthias
  • 3,160
  • 2
  • 24
  • 38
  • Thanks but it is not working for me .. This is what I am trying to do , I have an html tag input type file and I am trying to list the file on the device,,, On some version android I see a system filechooser but I have no Idea how to get the selected file... So, I decided to read the android folder .. Now I am not sure how to proceed – user2570135 Feb 28 '17 at 19:05
  • I downgrade to API 22 on android and now I am able to read the file... not sure what is happening.. – user2570135 Feb 28 '17 at 21:20