apache / cordova-plugin-file

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

Critical Issue: not working with external sdcard on phones like Samsung A7, A20, Note8, Huawei etc #350

Open azizbohra opened 4 years ago

azizbohra commented 4 years ago

Bug Report

Critical Issue. Need a solution. Please help me guys.

Problem

This plugin works perfectly with internal storage. But on some of the smartphones like Samsung A7 & one Huawei, it is not working with the external sdcard. Even after giving external storage path, it is always searching on internal storage.

What is expected to happen?

It should work with internal storage as well as external sdcard storage

What does actually happen?

Not working with external sdcard storage. Even after giving external storage path, it is always searching on internal storage.

Information

I have tested this plugin on Ionic1 project. Mount sdcard on your device. Check any file which exists on sdcard ($cordovaFile.checkFile), it will through error code 1 NOT_FOUND_ERR. Now copy same file on internal storage, it will work as expected. Because it is searching file only on internal storage not on external storage.

Command or Code

$cordovaFile.checkFile('file://sdcard/', "image.jpeg") .then(function (success) { // success }, function (error) { // error it will through error code 1 NOT_FOUND_ERR });

Environment, Platform, Device

Ionic1 Samsung A7, Huawei

breautek commented 4 years ago

Are you able to provide a minimal sample reproduction app without ionic to reproduce this issue? $cordovaFile.checkFile is not something that comes from Cordova so this will help us rule out that this is an ionic specific issue or if this is indeed an issue in the Cordova framework.

https://github.com/apache/cordova-contribute/blob/master/create-reproduction.md

Thanks.

azizbohra commented 4 years ago

https://github.com/apache/cordova-contribute/blob/master/create-reproduction.md

@breautek Here is a link to repository having reproduction sample. Please resolve this issue as soon as possible. https://github.com/azizbohra/reproduction-sample.git

This is an issue with smartphones like Samsung A7, A20, Note8, Huawei etc When you choose file from external sdcard and call resolveLocalFileSystemURL function, it will throw an error NOT_FOUND_ERROR with error code 1

I have used 3 cordova plugins to achieve this feature:

  1. cordova-plugin-file 6.0.2 "File"
  2. cordova-plugin-filechooser 1.2.0 "File Chooser"
  3. cordova-plugin-filepath 1.5.6 "cordova-plugin-filepath"

Platform Info: 8.1.2 (cordova-lib@8.1.1) android 7.1.4

This is my code:

var btnChooseFile = document.getElementById('chooseFile');

    btnChooseFile.addEventListener('click', function () {
        fileChooser.open(function (uri) { // this will give content://

            window.FilePath.resolveNativePath(uri, function (path) { // this will give file://

                window.resolveLocalFileSystemURL(path, function (fileEntry) {
                    alert('resolveLocalFileSystemURL success = ' + JSON.stringify(fileEntry));

                    fileEntry.file(function (file) {
                        var fileObj = {
                            path: fileEntry.nativeURL,
                            name: fileEntry.name,
                            mimeType: file.type,
                            size: file.size
                        };

                        alert('fileEntry success = ' + JSON.stringify(fileObj));
                    });
                }, function (error) {
                    alert('resolveLocalFileSystemURL error = ' + JSON.stringify(error));
                });
            }, function (error) {
                alert('resolveNativePath error = ' + JSON.stringify(error));
            });

        }, function (error) {
            alert(JSON.stringify(error));
        });
    });

WhatsApp Image 2019-09-29 at 10 01 01 AM

breautek commented 4 years ago

Thanks, I think this will help. I won't be able to test today because I dont have a phone with an sd card slot. But I'll try to spend a few minutes to at least identify the fault in my workplace on monday where I have access to other phones.

One other note though, I read other tickets that have a similar issue as you, and it was due to a missing permission declaration. In the meantime, I'd advise you to check your AndroidManifest.xml file (I don't have the exact filepath in front of me, but you should be able to find it if you search inside platforms/android/). Look for READ_EXTERNAL_PERMISSION permission being declared.

Sorry for being a bit vague in what to look for, I don't have a laptop in front of me at the moment. Alternatively, you can paste the 'AndroidManifest.xml` here so that I can take a look.

azizbohra commented 4 years ago

Here is my AndroidManifest.xml file and i can see read & write permission is there

`<?xml version='1.0' encoding='utf-8'?>

`

breautek commented 4 years ago

Unfortunately I'm unable to reproduce the issue using an emulator or any real devices that I have access to. This doesn't mean the issue doesn't exist obviously, My devices that have an SD card slot are old and aren't running the same OS version as you. This issue may be device specific. I was using the test app that you provided to do my tests.

Also worth noting that on my device, the sdcard path looks like file:///storage/extSdCard/ and not file://scard/ as shown in the original bug report. This may be confusing because the docs makes it appear like it should be /sdcard/.

azizbohra commented 4 years ago

Hello @breautek ,

I have tested the same app on Xiaomi M1 A1 having AndroidOne OS and getting the same issue while resolving local file system URL by (resolveLocalFileSystemURL). Even getting same issue on Samsung A7, A20, Note8, Huawei etc

But the twist is here that, I was testing day before yesterday and it was working file but now it is not working. The native URL i am getting is the same as before but when i am resolving native URL by resolveLocalFileSystemURL to get fileentry, it is throwing error NOT_FOUND error code 1. Even the app has storage permissions (Read & Write), but still not able to access it.

Second thing is very interesting, let me explain you in detail. Let's consider an example. Put one file in internal storage with name image1.png Put second file in external storage (sdcard) with name image2.png

Now try to access that file. when you select file from internal storage, it will give native URL like file:///storage/emulated/0/image1.png and it will be resolvable by resolveLocalFileSystemURL function.

when you select file from external storage (sdcard), it will give native URL like file:///sdcard/image2.png and it will not be resolvable by resolveLocalFileSystemURL function.

Now change the file name in internal storage from image1.png to image2.png. When you select file from external storage you will get same native URL file:///sdcard/image1.png, but now it will be resolvable by resolveLocalFileSystemURL

The conclusion is resolveLocalFileSystemURL is searching file only in internal storage even if the path is different. But it was supposed to search file on external storage (sdcard).

I am searching for a solution. Please help me bro.

breautek commented 4 years ago

Ok so, if I understand correctly, the issue occurs when you have 2 identical filenames, one on the internal storage, and the other on the external storage.

Then if you use resolveLocalFileSystemURL on the sdcard file, it will fail?

In my testing, my sdcard/internal storage definitely didn't had two of the same files with the files I was testing with. So later tonight I can try again with this kind of environment.

azizbohra commented 4 years ago

@breautek

No i mean to say, if you have same file on internal or external device storage then it will work because in the both the cases resolveLocalFileSystemURL is searching only in internal storage.

If you don't have same file on external or internal then it will not work. It will work only if you are selecting file from internal storage.

The conclusion is, in both the cases it is not working at all with external storage. resolveLocalFileSystemURL throwing an error NOT_FOUND !!!!!!

Just now i have tested it on Galaxy Note3 and it is working fine. It is giving me this file:///storage/extSdCard/ URL for external storage files.

breautek commented 4 years ago

Just now i have tested it on Galaxy Note3 and it is working fine. It is giving me this file:///storage/extSdCard/ URL for external storage files.

On my device that has an sdcard slot, this is what the path also looks like for me.

I'm not familiar with this codebase, but I'll take a look to see if I can find any bad assumptions... But I can only do this later tonight unfortunately. Of course you are more than welcome to poke around the native code yourself and add logging to the native code to try to find any discrepancies between the phones that do work vs the phones that don't work.

So far the pattern appears to be phones that provide a file:///sdcard/ path doesn't work, but phones that provide a file:///storage/extSdCard/ does work. I don't have access to a device that produces a file:///sdcard/ path, so if the pattern is related then I won't be able to reproduce and therefore I cannot be confident if I make any changes, it will actually solve any issue.

The method in question is located at https://github.com/apache/cordova-plugin-file/blob/74a46467a081a87bb69b3f2518cbb1db5375028f/src/android/FileUtils.java#L707

This is probably the area of interest: https://github.com/apache/cordova-plugin-file/blob/74a46467a081a87bb69b3f2518cbb1db5375028f/src/android/FileUtils.java#L721-L735

If it doesn't enter the if (fs.exists(inputURL)) { block, then it will end up throwing a file not found error.

azizbohra commented 4 years ago

I am not sure about that, I think file:///sdcard & file:///storage/extSdCard/ both are working but on few devices resolveLocalFileSystemURL throwing an error NOT_FOUND.

breautek commented 4 years ago

This issue may be related to

https://issues.apache.org/jira/browse/CB-10883

Which has a pending PR for a fix for https://github.com/apache/cordova-plugin-file/pull/173, but currently it is in a conflicted state and had no activity for a year.

The Jira issue states that this is an issue starting from Android 6 and above, which will explain why I couldn't reproduce the issue on my test device.

I found my work's android 6 device, so I'll bring that home tonight and test using that device.

breautek commented 4 years ago

Just tested with the Samsung Galaxy s5, running Android 6. I have a sopy of images stored in both the internal storage and sd card but I'm still unable to reproduce the issue.

Internal storage path: file:///storage/emulated/0/Pictures/Screenshots/Screenshot_2018-01-12-16-11-26.png

SD Card: file:///storage/661F-EA5B/Screenshot_2018-01-12-16-11-26.png SD Card: file:///storage/661F-EA5B/Pictures/Screenshots/Screenshot_2018-01-12-16-11-26.png

breautek commented 4 years ago

@azizbohra You can try running logcat to find out more information Perhaps using:

adb logcat *:S *:W *:E

Which will silent all logs except for warnings and errors.

You can also look in the source files for TAG or LOG_TAG strings and filter by them by doing:

adb logcat *:S *:W *:E <tag1> <tag2>

azizbohra commented 4 years ago

@breautek

Hello i am sharing the logs here, but i don't think this logs contains anything related to this issue.

Screen Shot 2019-10-05 at 9 31 59 AM Screen Shot 2019-10-05 at 9 32 36 AM

azizbohra commented 4 years ago

adb logcat :S :W *:E TAG LOG_TAG

Logs when selecting from internal storage

10-05 09:39:18.934 570 617 E ANDR-PERF-MPCTL: Invalid profile no. 0, total profiles 0 only 10-05 09:39:18.984 570 617 E ANDR-PERF-OPTSHANDLER: Perflock resource /sys/class/devfreq/soc:qcom,llccbw/min_freq not supported 10-05 09:39:18.984 570 617 E ANDR-PERF-RESOURCEQS: Failed to apply optimization [12, 0] 10-05 09:39:18.985 570 617 E ANDR-PERF-OPTSHANDLER: Failed to read /sys/class/kgsl/kgsl-3d0/force_no_nap 10-05 09:39:18.985 570 617 E ANDR-PERF-RESOURCEQS: Failed to apply optimization [10, 7] 10-05 09:39:18.985 570 617 E ANDR-PERF-OPTSHANDLER: Perflock resource /sys/class/devfreq/soc:qcom,llccbw/min_freq not supported 10-05 09:39:18.985 570 617 E ANDR-PERF-RESOURCEQS: Failed to apply optimization [12, 0] 10-05 09:39:18.985 570 617 E ANDR-PERF-OPTSHANDLER: Failed to read /sys/class/kgsl/kgsl-3d0/force_no_nap 10-05 09:39:18.985 570 617 E ANDR-PERF-RESOURCEQS: Failed to apply optimization [10, 7] 10-05 09:39:19.303 2902 3437 E ActivityTrigger: activityStartTrigger: not whiteListedcom.android.documentsui/com.android.documentsui.picker.PickActivity/28 10-05 09:39:19.303 2902 3437 E ActivityTrigger: activityResumeTrigger: not whiteListedcom.android.documentsui/com.android.documentsui.picker.PickActivity/28 10-05 09:39:19.307 570 617 E ANDR-PERF-OPTSHANDLER: Perflock resource /sys/class/devfreq/soc:qcom,llccbw/min_freq not supported 10-05 09:39:19.308 570 617 E ANDR-PERF-RESOURCEQS: Failed to apply optimization [12, 0] 10-05 09:39:19.308 570 617 E ANDR-PERF-OPTSHANDLER: Failed to read /sys/class/kgsl/kgsl-3d0/force_no_nap 10-05 09:39:19.308 570 617 E ANDR-PERF-RESOURCEQS: Failed to apply optimization [10, 7] 10-05 09:39:19.329 2902 3437 E ActivityTrigger: activityResumeTrigger: not whiteListedcom.android.documentsui/com.android.documentsui.picker.PickActivity/28 10-05 09:39:19.330 570 617 E ANDR-PERF-OPTSHANDLER: Perflock resource /sys/class/devfreq/soc:qcom,llccbw/min_freq not supported 10-05 09:39:19.330 570 617 E ANDR-PERF-RESOURCEQS: Failed to apply optimization [12, 0] 10-05 09:39:19.330 570 617 E ANDR-PERF-OPTSHANDLER: Failed to read /sys/class/kgsl/kgsl-3d0/force_no_nap 10-05 09:39:19.330 570 617 E ANDR-PERF-RESOURCEQS: Failed to apply optimization [10, 7] 10-05 09:39:21.987 570 617 E ANDR-PERF-OPTSHANDLER: Sched boost lock is already acquired. Concurrency not supported for this resource 10-05 09:39:21.987 570 617 E ANDR-PERF-RESOURCEQS: Failed to apply next pending optimization [3, 0] 10-05 09:39:23.200 2902 3437 E ActivityTrigger: activityResumeTrigger: not whiteListedcom.example.hello/com.example.hello.MainActivity/10000 10-05 09:39:23.202 570 617 E ANDR-PERF-MPCTL: Invalid profile no. 0, total profiles 0 only 10-05 09:39:23.216 570 617 E ANDR-PERF-OPTSHANDLER: Perflock resource /sys/class/devfreq/soc:qcom,llccbw/min_freq not supported 10-05 09:39:23.216 570 617 E ANDR-PERF-RESOURCEQS: Failed to apply optimization [12, 0] 10-05 09:39:23.216 570 617 E ANDR-PERF-OPTSHANDLER: Failed to read /sys/class/kgsl/kgsl-3d0/force_no_nap 10-05 09:39:23.216 570 617 E ANDR-PERF-RESOURCEQS: Failed to apply optimization [10, 7] 10-05 09:39:30.650 2196 2289 E storaged: getDiskStats failed with result NOT_SUPPORTED and size 0 10-05 09:39:35.584 546 6346 E se.dirac.effect: first processing 10-05 09:39:35.589 570 617 E ANDR-PERF-MPCTL: Invalid profile no. 0, total profiles 0 only 10-05 09:39:35.589 546 4154 E msm8916_platform: platform_check_backends_match: Invalid snd_device = 10-05 09:39:35.589 546 4154 E audio_hw_primary: start_dsm_feedback_process: pcm device id 43 10-05 09:39:35.609 546 4154 E soundtrigger: audio_extn_sound_trigger_update_stream_status: invalid input device 0x0, for event 3 10-05 09:39:35.609 546 4154 E msm8916_platform: platform_get_snd_device_backend_index: BE DAI Name Table is not present 10-05 09:39:35.609 546 4154 E audio_hw_utils: send_app_type_cfg_for_device: Couldn't get the backend index for snd device speaker ret=-14 10-05 09:39:41.301 546 1147 E soundtrigger: audio_extn_sound_trigger_update_stream_status: invalid input device 0x0, for event 2

Logs when selecting files from sdcard

10-05 09:41:57.976 570 617 E ANDR-PERF-OPTSHANDLER: Perflock resource /sys/class/devfreq/soc:qcom,llccbw/min_freq not supported 10-05 09:41:57.976 570 617 E ANDR-PERF-RESOURCEQS: Failed to apply optimization [12, 0] 10-05 09:41:57.977 570 617 E ANDR-PERF-OPTSHANDLER: Failed to read /sys/class/kgsl/kgsl-3d0/force_no_nap 10-05 09:41:57.977 570 617 E ANDR-PERF-RESOURCEQS: Failed to apply optimization [10, 7] 10-05 09:41:57.977 570 617 E ANDR-PERF-OPTSHANDLER: Perflock resource /sys/class/devfreq/soc:qcom,llccbw/min_freq not supported 10-05 09:41:57.977 570 617 E ANDR-PERF-RESOURCEQS: Failed to apply optimization [12, 0] 10-05 09:41:57.977 570 617 E ANDR-PERF-OPTSHANDLER: Failed to read /sys/class/kgsl/kgsl-3d0/force_no_nap 10-05 09:41:57.977 570 617 E ANDR-PERF-RESOURCEQS: Failed to apply optimization [10, 7] 10-05 09:41:58.380 2902 11530 E ActivityTrigger: activityStartTrigger: not whiteListedcom.android.documentsui/com.android.documentsui.picker.PickActivity/28 10-05 09:41:58.381 2902 11530 E ActivityTrigger: activityResumeTrigger: not whiteListedcom.android.documentsui/com.android.documentsui.picker.PickActivity/28 10-05 09:41:58.395 570 617 E ANDR-PERF-OPTSHANDLER: Perflock resource /sys/class/devfreq/soc:qcom,llccbw/min_freq not supported 10-05 09:41:58.395 570 617 E ANDR-PERF-RESOURCEQS: Failed to apply optimization [12, 0] 10-05 09:41:58.395 570 617 E ANDR-PERF-OPTSHANDLER: Failed to read /sys/class/kgsl/kgsl-3d0/force_no_nap 10-05 09:41:58.395 570 617 E ANDR-PERF-RESOURCEQS: Failed to apply optimization [10, 7] 10-05 09:41:58.407 2902 11530 E ActivityTrigger: activityResumeTrigger: not whiteListedcom.android.documentsui/com.android.documentsui.picker.PickActivity/28 10-05 09:41:58.408 570 617 E ANDR-PERF-OPTSHANDLER: Perflock resource /sys/class/devfreq/soc:qcom,llccbw/min_freq not supported 10-05 09:41:58.408 570 617 E ANDR-PERF-RESOURCEQS: Failed to apply optimization [12, 0] 10-05 09:41:58.409 570 617 E ANDR-PERF-OPTSHANDLER: Failed to read /sys/class/kgsl/kgsl-3d0/force_no_nap 10-05 09:41:58.409 570 617 E ANDR-PERF-RESOURCEQS: Failed to apply optimization [10, 7] 10-05 09:41:59.814 546 6346 E se.dirac.effect: first processing 10-05 09:41:59.817 546 4154 E msm8916_platform: platform_check_backends_match: Invalid snd_device = 10-05 09:41:59.817 546 4154 E audio_hw_primary: start_dsm_feedback_process: pcm device id 43 10-05 09:41:59.830 546 4154 E soundtrigger: audio_extn_sound_trigger_update_stream_status: invalid input device 0x0, for event 3 10-05 09:41:59.831 546 4154 E msm8916_platform: platform_get_snd_device_backend_index: BE DAI Name Table is not present 10-05 09:41:59.831 546 4154 E audio_hw_utils: send_app_type_cfg_for_device: Couldn't get the backend index for snd device speaker ret=-14 10-05 09:42:01.664 2902 11530 E ActivityTrigger: activityResumeTrigger: not whiteListedcom.example.hello/com.example.hello.MainActivity/10000 10-05 09:42:01.665 570 617 E ANDR-PERF-MPCTL: Invalid profile no. 0, total profiles 0 only 10-05 09:42:01.679 570 617 E ANDR-PERF-OPTSHANDLER: Perflock resource /sys/class/devfreq/soc:qcom,llccbw/min_freq not supported 10-05 09:42:01.679 570 617 E ANDR-PERF-RESOURCEQS: Failed to apply optimization [12, 0] 10-05 09:42:01.679 570 617 E ANDR-PERF-OPTSHANDLER: Failed to read /sys/class/kgsl/kgsl-3d0/force_no_nap 10-05 09:42:01.679 570 617 E ANDR-PERF-RESOURCEQS: Failed to apply optimization [10, 7] 10-05 09:42:07.519 546 1147 E soundtrigger: audio_extn_sound_trigger_update_stream_status: invalid input device 0x0, for event 2

azizbohra commented 4 years ago

@breautek

I have test this paid plugin even that is also not working https://github.com/jennielynshapiro/cordova-plugin-filestack

breautek commented 4 years ago

By the looks of that plugin, it doesn't appear to handle external storage whatsoever, or at least it lacks declaring the required permissions to use external storage.

Afraid to say I don't think I can be much help unless if I can reproduce this issue, and with all my devices at disposal that just doesn't happen. Afaik these issues are device specific.

Later tonight if I find time I'll try to use a virtual external storage on my Android 9 phone to see if it gets reproduced then. But if it doesn't, then I'm out of ideas...

And to acknowledge your logs, I don't see anything interesting either...

graphefruit commented 3 years ago

Stumbled across this issue aswell. Any new information here? Checked it out with my old Note 2. Url can be resolved, but reading the data or copy the file is not working

distante commented 3 years ago

Was this ever fixed? I have this same problem, When I try to check if a file exist on the SD card it always returns "not found" Tested with a Motorola Moto G5 with Android 8

Path example: file:///sdcard/Music/[MemoryCard] DragonBall GT - OP1 - Dan Dan Kokoro Hikareteku.mp3

rnschulenburg commented 3 years ago

Same here. Has anyone found a fix? Cannot find a solution

Dharmesh2015 commented 3 years ago

Hi , i am using one+ 6 mobile with the latest os. and encounter my application is not able to read files with the same issue? if you run Cordova.plugins.diagnostic.getExternalSdCardDetails it is not fetching /sdcard/ it is only fetching /emulated/0/ which is pointing to internal storage, I am just want to list the folder however due to this it's not working, what else we need to identify please let me know will provide it

tried on OnePluse5,6,6T all phone however it's not working

any fix so far ??