Nativescript: Create and Save file failing on Android 10

Created on 25 Sep 2019  路  11Comments  路  Source: NativeScript/NativeScript

Environment
Component nativescript has 6.1.2 version.
Component tns-core-modules has 6.1.1
Component tns-android has 6.1.1
Component tns-ios has 6.1.1

BUG
The issue is only with Android 10.
Trying to create a folder or trying to save a file to DIRECTORY_DOWNLOADS fails.

     createFolderForFiles() {
        try {
            /** Getting the Android Download directory */
            this.androidDownloadsPath =android.os.Environment.getExternalStoragePublicDirectory(android.os.Environment.DIRECTORY_DOWNLOADS).toString();
            /**  Creating or getting a folder in Android Download directory */
            this.path = fs.path.join(this.androidDownloadsPath, "myFolder");
            this.folder = fs.Folder.fromPath(this.path);
        } catch (error) {
            console.log("ERROR: Creating folder: " + error);
        } 
    }

RESULT: ERROR: Creating folder: Failed to create new java File for path /storage/emulated/0/Download/myFolder

Similarly, trying to download and open a file with Intent Chooser fails with the following error.

       try {
                    let intent = new android.content.Intent(android.content.Intent.ACTION_VIEW);
                    // let context = application.android.currentContext;
                    let context = application.android.context;
                    let mimeType = this.findExtension(extension);
                    /*
                    mimeType = mimeType.getMimeTypeFromExtension(extension.replace(".", 
                    "").toLowerCase());
                   */
                    /** Grant permission for third party apps to read the content of this file */
                    intent.addFlags(android.content.Intent.FLAG_ACTIVITY_NEW_TASK);
                    intent.addFlags(android.content.Intent.FLAG_GRANT_READ_URI_PERMISSION);

                    let chooserIntent = android.content.Intent.createChooser(intent, title);
                    chooserIntent.addFlags(android.content.Intent.FLAG_ACTIVITY_NEW_TASK);
              chooserIntent.addFlags(android.content.Intent.FLAG_GRANT_READ_URI_PERMISSION);
                    /** Set the file content URI using Android native APIs */
                    intent.setDataAndType(androidx.core.content.FileProvider.getUriForFile(context, context.getApplicationContext().getPackageName() + ".fileprovider", new java.io.File(file.path)), mimeType);
                    context.startActivity(chooserIntent);
                } catch (e) {
                    console.log("ERROR: Cannot open file " + e);
                    return false;
                }

ERROR: Cannot open file Error: Cannot save file with path: /storage/emulated/0/Download/myFolder/MyFile.pdf.
Note: Observe the '.' after the extension(.pdf.)

Are there any new additional changes on Android? Or am I missing something?
Please help. Thank you.

android

Most helpful comment

Hi @tsonevn,

Yes this works with Just had to clean platform and re run.

All 11 comments

Hi @AmithRamesh,
Make sure that you have added the needed permissions to write and read from the storage.

Hi @tsonevn,
Thank you.
I have added the following permissions to my manifest file:

<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>

Able to save and open the file in Android 9 and below. Issue only with Android 10.
And also now, added the following as specified in the Android doc you shared:

<manifest ... >
  <!-- This attribute is "false" by default on apps targeting
       Android 10 or higher. -->
  <application android:requestLegacyExternalStorage="true" ... >
    ...
  </application>
</manifest>

Basically I am not an Android developer, so thought I will opt out of scoped storage concept until I figure out what it is. But facing the same errors even after making this change.

So does that mean I need to add this permission - ACCESS_MEDIA_LOCATION ??
If I use the above permission, should I use the android:requestLegacyExternalStorage as well?

I am confused!!

I am now facing a similar issue that Image src="path to image media file" does not show up. This is only an issue on Android API Level 29

When I add

So my question: does Nativescript 6.1.2 already support Android 10 scoped storage?

Hi @tsonevn,

Yes this works with Just had to clean platform and re run.

@AmithRamesh Have you solved the issue by adding

@SaifulZiko setting android:requestLegacyExternalStorage="true" worked just fine for me

setting android:requestLegacyExternalStorage="true" worked , thanks

now after adding android:requestLegacyExternalStorage="true" also not create file or mkdir , dont know but working inside picture folder but not on the root folder dnt know ??

Getting 'IllegalStateException: not one of standard directory' while downloading a file on Android 10.
Also added android:requestLegacyExternalStorage="true"

I am to get select multiple contact .That is working fine andorid 9.0 till. But Android version 10 not working
android:requestLegacyExternalStorage="true" -- This also not working

Hi,
I have
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
As well as
android:requestLegacyExternalStorage="true"

But can't download files to Download folder, any help will be apricated ..
Android 10 + 11..

thanks

Was this page helpful?
0 / 5 - 0 ratings