apache / cordova-plugin-file

Apache Cordova File Plugin
https://cordova.apache.org/
Apache License 2.0
741 stars 756 forks source link

Plugin is not listing documents(.pdf,.docx etc) #454

Closed GAGANsinghmsitece closed 1 year ago

GAGANsinghmsitece commented 3 years ago

Bug Report

Problem

What is expected to happen?

Plugin should log directories while log all the pdf files present in internal storage(android) on android.

What does actually happen?

It list files like .jpeg,.mp3,.mp4 but do not list any document like .pdf,.docx

Information

I tried following code on one UI 3.0(android 11):-

let readfiles={
       'read':function(path){
      window.resolveLocalFileSystemURL(path,
        function (fileSystem) {
            var reader = fileSystem.createReader();
            reader.readEntries(
                function (entries) {
                    console.log(entries);
                    for(var i=0;i<entries.length;i++)
                        if(entries[i].isDirectory===true)
                            readfiles.read(entries[i].nativeURL);
                        else{
                            if(entries[i].nativeURL.endsWith(".pdf")===true)
                                console.log(entries[i].nativeURL);
                        }
                },
                function (err) {
                    console.log(err);
                });
        }, function (err) {
            console.log(err);
        }
    );
      }
    }
    readfiles.read(cordova.file.externalRootDirectory);

Environment, Platform, Device

I am building this project on ubuntu 20.04LTS and the device I used for testing is Galaxy M31 with One UI 3.0(android 11).

Version information

Cordova: 10.0.0 cordova-plugin-file: 6.0.2

Checklist

jfoclpf commented 3 years ago

Which Android version (or API level) are you using?

GAGANsinghmsitece commented 3 years ago

I'm using API level 30, for now I solved this error using requestLegacyExternalStorage

jfoclpf commented 3 years ago

Could you kindly tell me how exactctly did you use requestLegacyExternalStorage?

In config.xml? How? Thanks

jfoclpf commented 3 years ago

@GAGANsinghmsitece have you tested in Android 11? I don't think that flag works in Android 11, just in Android 10

More information here: https://github.com/apache/cordova-plugin-file/issues/426

GAGANsinghmsitece commented 3 years ago

I tried this in one ui 3.0 which is based on android 11 and it works. If you looked at https://developer.android.com/about/versions/11/privacy/storage . It says,

Apps that run on Android 11 but target Android 10 (API level 29) can still request the requestLegacyExternalStorage attribute. This flag allows apps to temporarily opt out of the changes associated with scoped storage, such as granting access to different directories and different types of media files. After you update your app to target Android 11, the system ignores the requestLegacyExternalStorage flag.

Which means if your app targets API level 29, it can still request access to all files in android 11 using the flag. Still, it's a temporary hack as in future version, it will cause more trouble. Hope this plugin include these changes in future versions. I created the app in feb 2021 and it is currently working Most probably, you haven't asked for permission before accessing file system

rejneesh1 commented 2 years ago

Still facing the same issue for pdf files , any solution?

victorvhpg commented 2 years ago

Any update?

breautek commented 2 years ago

As of API 29, scoped storage was introduced. API 29 offered a flag to opt out of scoped storage, but in API 30, that flag is ignored and scoped storage is always enforced.

By default, without the READ_EXTERNAL_STORAGE permission, any files that is not created by your app will be hidden (e.g. attempting to read them will result in a file not found error). I have a fork that might help with this issue.

Listing directories never checked/request the READ_EXTERNAL_STORAGE permission, and I have a PR that addresses this.

danicarla commented 2 years ago

As of API 29, scoped storage was introduced. API 29 offered a flag to opt out of scoped storage, but in API 30, that flag is ignored and scoped storage is always enforced.

By default, without the READ_EXTERNAL_STORAGE permission, any files that is not created by your app will be hidden (e.g. attempting to read them will result in a file not found error). I have a fork that might help with this issue.

Listing directories never checked/request the READ_EXTERNAL_STORAGE permission, and I have a PR that addresses this.

How do I use your "FORK"?

breautek commented 1 year ago

How do I use your "FORK"?

My branch has been deleted since then, but my PR was merged in and released in v7.0.0

However, Android Scoped Storage makes it so you cannot discover files that wasn't written by your app. Listing files in the external directories (e.g. inside /storage/emulated/0/ or /sdcard/) will only list files that was written by your app. Other files written by other apps will not be listed anymore.

For this reason, I'm closing as not a bug.