apache / cordova-plugin-file

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

Download file (write) problem in API 30 - Android 11 #511

Closed dec closed 2 years ago

dec commented 2 years ago

Bug Report

Problem

What is expected to happen?

Download a file (basically use XMLHttpRequest and then write it to the file system).

What does actually happen?

The file is not written in the file system (but before Android 11 - API 29 is perfectly written).

Information

Hello to all! I know this problem start with Android 11 and already read here that lot of people have the same problem. The point is that anything that I try is working as expected. Specifically, to me it's not a problem to put the "MANAGE_EXTERNAL_STORAGE" permission in the AndroidManifest.xml, but, even with this permission set, the file is not downloaded as expected.

Apparently, with the referred permission, I can go to the Android settings and can manually grant for the app the permission to "manage all the files", however, even with this, the file is not downloaded / written has expected. I can note that without the referred permission, I can't set the "manage all the files" permission for the app from the Android settings.

The directoy in which I want to download the file is "cordova.file.externalRootDirectory + 'Download'" and I try also with other directories like "cordova.file.externalRootDirectory + 'Documents'" with the same result: the file is not downloaded. Apparently the problem is to simply reach the "cordova.file.externalRootDirectory".

If I decide to open a new "bug / issue" is because I want to be ready with the new "storage policy" establish by Android 11, so for that reason I add the "MANAGE_EXTERNAL_STORAGE" permission in the Android manifest, however, as I say before, even with this permission the things don't work, so, my question can be: How we can be ready to use the new "storage policy" in Android 11?

Thanks in advance for any kind of help on this issue! If you need any kind of information, please, just post here and I will try to provide it as quickly as possible!

Source code

Please, see the attached file.

download.zip

Environment, Platform, Device

Android 11 (API 30)

Version information

Cordova CLI 10.0.1 - cordova-plugin-file Android Target SDK API 30 Microsoft Windows 11

Checklist

dec commented 2 years ago

Hello to all,

This is a bit crazy... but I want to comment it here... because may help. Testing my "DownloadFile" function in a "debug" APK here in Android 11 and API 30, I can't see it's not working as before. HOWEVER, testing the same app, which is already published in the Google Play Store, and, again, with Android 11 and API 30... to my surprise the "DownloadFile" function works as expected...

The only difference to me is that the app who don't work is a "debug" APK... and the published one is a "release" app bundle... can this make any difference? Because certainly the last one works as expected... but the first one do not work... as I mentioned in my first post... Can be any difference between a "debug" and a "release" app here?

dec commented 2 years ago

Hello to all,

Well... here are more information about this issue, that may help to someone. I have some good news... first of all, establish one thing: my "DownloadFile" function is trying to download to "cordova.file.externalRootDirectory + 'Download'". I mention this because I am not trying nor test to download directly to the root folder, but to the "Download" subfolder.

Well. Saying that... what I can discover is that in a "release APK" that I prepare of the app the "DownloadFile" function is working as expected in Android 11 + API 30! So apparently the problem exists in the "debug APK" and not in the "release APK". I need to try this, because, certainly the published app (Android 11 + API 30) works, and, of course, the published app is a release and not a debug version.

So this is what I can say... apparently my "DownloadFile" function works as expected if I try it in a "release APK" and don't work as expected if I try it in a "debug APK". On the other hand, I also want to say that I am not trying with the "MANAGE_EXTERNAL_STORAGE" permission.

So to place here a conclusion, at least from my side, the download of a file in the "cordova.file.externalRootDirectory + 'Download'" folder, works as expected talking about a "release APK". I did not try to download the file in the root directory, but, this is not needed to me, since I always download inside the "Download" folder.

Maybe this can help to someone who are experiment this kind of issue: try your app after make a "release APK", in other words, don't try your app in a "debug APK", because apparently in this last case the problem occur.

dec commented 2 years ago

Hello to all again...

Here with more information about this "Download issue"... this starts to be like a bad joke... apparently I can download PDF files... but not PNG files...

I certainly read something about that now the type of the files matter for Android 11 + API 30... and that's apparently truth...

So I continue asking for some help... that is... how to be ready to download ANY KIND of files in Android 11 + API 30... :-(

Thanks in advance for any possible help!

dlazzy commented 2 years ago

Same problem here, to record video or audio (with plugin cordova-plugin-media) it gives error code 1 on Android 10 and +. The PlayStore is refusing the app since there is this MANAGE_EXTERNAL_STORAGE permission. Big problems incoming.

dec commented 2 years ago

Hello to all!

Same problem here, to record video or audio (with plugin cordova-plugin-media) it gives error code 1 on Android 10 and +. The PlayStore is refusing the app since there is this MANAGE_EXTERNAL_STORAGE permission. Big problems incoming.

The refusing result in the store can be a problem, certainly, however, the point is that even with the MANAGE_EXTERNAL_STORAGE permission set, in my case, the download operation is not working either... even more, I go to the app's permissions in the Android settings and check the "allow to manage device files", and, even with this checked, the download operation continue failing... :-(

dlazzy commented 2 years ago

Hello to all!

Same problem here, to record video or audio (with plugin cordova-plugin-media) it gives error code 1 on Android 10 and +. The PlayStore is refusing the app since there is this MANAGE_EXTERNAL_STORAGE permission. Big problems incoming.

The refusing result in the store can be a problem, certainly, however, the point is that even with the MANAGE_EXTERNAL_STORAGE permission set, in my case, the download operation is not working either... even more, I go to the app's permissions in the Android settings and check the "allow to manage device files", and, even with this checked, the download operation continue failing... :-(

I don't know what's going on with all of these plugins... They works really well before but now! Unfortunately I don't have skills to try fixing native side. I hope you will find a solution for the download problem, I'll watch this issue.

dlazzy commented 2 years ago

Just a little update :

I was not trying to download a file @dec, but to record and listen a recorded audio. This issue is linked to the https://github.com/apache/cordova-plugin-media. To resolve my problem with the permission and error code 1 when trying to record, I've updated the cordova cli to 11.0.0 (npm install -g cordova@11.0.0) then updated the android platform version :

cordova platform remove android && cordova platform add android@10.1.1 --save

And reinstall plugin media with cordova plugin remove cordova-plugin-media && cordova plugin add cordova-plugin-media@5.0.4

Finally in code, instead of using some externalDataDirectory with file plugin use cacheDirectory :

this.filePath = this.file.cacheDirectory + 'temp.3gp';
this.recorder = this.media.create(this.filePath);
this.recorder.startRecord();
...

For the "listen" part just make sure that your audio file is coming from https:// url.

I can now record, play and push to Google Playstore since the MANAGE_EXTERNAL_STORAGE is not present anymore.

dec commented 2 years ago

Hello to all,

Certainly, @dlazzy maybe the "cache" folder has no problems. But the point is to use the "external" folder, not because we like it, but, because what we want to get is a download file "for the user", that is, the functionallity of the app is to let the user to download a file, so, the file must be downloaded in a place which the user can reach: for this reason we use the "Download" folder, because it's the expected directory (for the user) to the downloaded files.

dlazzy commented 2 years ago

Hello to all,

Certainly, @dlazzy maybe the "cache" folder has no problems. But the point is to use the "external" folder, not because we like it, but, because what we want to get is a download file "for the user", that is, the functionallity of the app is to let the user to download a file, so, the file must be downloaded in a place which the user can reach: for this reason we use the "Download" folder, because it's the expected directory (for the user) to the downloaded files.

Sure, you are right this is a problem :disappointed: I hope this will be resolved I need this too on one of my application.

xtof974 commented 2 years ago

Hello,

I have exactly the same issue. My app worked perfectly : donwload file + store it on "cordova.file.externalRootDirectory + 'Download'". But now, it's broken.

Any updates on this issue ?