react-native-zip-archive icon indicating copy to clipboard operation
react-native-zip-archive copied to clipboard

couldn't extract files under the Download directory on Android 10

Open Ashkan-Oliaie opened this issue 4 years ago • 8 comments

Describe the bug I upgraded my react-native application from 0.62 to 0.63 and I get couldn't open the file while unzipping .

Additional context the only way I could fix it was by downgrading to the version I was using , 0.62 . maybe some other changes are required in order to get this to work.

Ashkan-Oliaie avatar Oct 14 '20 04:10 Ashkan-Oliaie

No, you don't need to apply any changes to upgrade rn from 62 to 63 on the module. And I just tested with the test app https://github.com/plrthink/RNZATestApp but can't get the error you're facing. Can you provide an app that can produce the error? That would be much helpful.

plrthink avatar Oct 18 '20 08:10 plrthink

I have this issue as well, I noted after adding a System output to the android code I got the following message: System.out: java.io.FileNotFoundException: /storage/emulated/0/Download/projects/my-file-name.abc

The file is a zip file, with a non-standard extension. Were restrictions added related to extension? I am wondering if it's also possible it's related to this: https://medium.com/@sriramaripirala/android-10-open-failed-eacces-permission-denied-da8b630a89df

jstopchick avatar Nov 19 '20 20:11 jstopchick

I have this issue as well, I noted after adding a System output to the android code I got the following message: System.out: java.io.FileNotFoundException: /storage/emulated/0/Download/projects/my-file-name.abc

The file is a zip file, with a non-standard extension. Were restrictions added related to extension? I am wondering if it's also possible it's related to this: https://medium.com/@sriramaripirala/android-10-open-failed-eacces-permission-denied-da8b630a89df

It appears the link above was the issue for me, when using the "Downloads" directory on Android, there are restrictions related to the way zip files can be read. The temporary fix for me was adding android:requestLegacyExternalStorage="true".

jstopchick avatar Nov 19 '20 20:11 jstopchick

@jstopchick Ok, but still can you provide a reproducible app? So I can easily follow the way.

plrthink avatar Nov 20 '20 05:11 plrthink

@plrthink - Here are the steps to reproduce, I've confirmed this issue happens on my Android Pixel phone.

  • On the phone, the default download folder should be called "Download". Place a zip file in that folder. In my case it had a non-standard extension: testZipFile.abc

  • Also, create a folder called "unzipTest" within the download folder. These will be the test files/folders.

  • Create a new react-native project, (using react-native 0.63.3/node v12.13.1) for this example call it "unziptest" "react-native init unziptest"

  • Go into "~localFolder\unziptest\android\app\src\main\AndroidManifest.xml"

-Add the following user permission lines: <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /> <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />

  • Take the attached zip file and extract the three files into the root project folder, overwrite the default project files. (App.js, index.js, package.json)

  • run "npm install" from the root project folder

  • run "react-native link react-native-fs" to link react-native-fs

  • Attempt to run the application with the following command: "react-native run-android"

  • The application will open on your device. There should be a "TEST UNZIP" button. Tap that to see the "cannot read file" error.

The fix per the link above.

  • Close the debugger and application.

  • Go into "~localFolder\unziptest\android\app\src\main\AndroidManifest.xml"

  • Add the following property to the <application> tag inside of the <manifest> tag: android:requestLegacyExternalStorage="true"

  • Run the application with the following command: "react-native run-android"

  • Tapping the unzip button should now show an alert, indicating it worked.

According to the link this is temporary solution. It appears the article also recommends using "openFileDescriptor" to "make a copy of the file that the user picks in the media gallery and add it to your application cache directory."

Hope this helps!

unziptest-core-files.zip

jstopchick avatar Nov 20 '20 11:11 jstopchick

I can confirm this is an issue but honestly, I don't know how to solve this. Even more, I'm not sure this is a problem that we should fix since it seems like a reasonable restriction that would be followed.

So I need more time to dive into and hope the community can contribute their opinions.

plrthink avatar Nov 27 '20 06:11 plrthink

For now, I just change the code to let the error be more clear about the file permission issue, you can try to install the current beta channel by yarn add react-native-zip-archive@beta.

plrthink avatar Nov 27 '20 08:11 plrthink

After some searching, I found that even though we can use the android:requestLegacyExternalStorage="true" but after targeting the Android 11(API Level 30) we have no chance to access files directly in the Download folder according to this article https://medium.com/androiddevelopers/android-11-storage-faq-78cefea52b7c#a018:

Are there any further restrictions to using Storage Access Framework in Android 11 as compared to Android 10?

Apps that target Android 11 (API level 30) and use Storage Access Framework will no longer be able to grant access to directories, such as the root directory of the SD card and the Download directory.

So I think this can only be solved by altering the location where the app stores files. Therefore, we may only make the error message more clear and highlight this behavior in the readme, but not changing the code.

But I'm also open on this, discussions are welcome.

plrthink avatar Nov 27 '20 09:11 plrthink

On android 13, the Downloads folder is still accessible by react-native-fs. However this issue still exists when trying to unzip a zip with a custom extension from the Downloads folder

GNUGradyn avatar Nov 27 '23 00:11 GNUGradyn

On android 13, the Downloads folder is still accessible by react-native-fs. However this issue still exists when trying to unzip a zip with a custom extension from the Downloads folder

By custom extension you mean?

plrthink avatar Dec 11 '23 02:12 plrthink

E.g. trying to unzip a zip with the extension .example instead of .zip

GNUGradyn avatar Dec 11 '23 03:12 GNUGradyn