Home > Enterprise >  Android API 28, save file in Download folder without user interaction
Android API 28, save file in Download folder without user interaction

Time:11-11

we're developping an app that will run as a service. One of the feature would be to download file at given URL (ex PDF) and save it into the download folder so user can load it from a specific application (Avenza Maps).

All the download process should be without any user interaction since it's by a service that run in the background.

i've added the following permission:

AndroidManifest.xml

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

<application
    android:requestLegacyExternalStorage="true"

whatever i'll try i got the following error

java.io.FileNotFoundException: /storage/emulated/0/Download/name.pdf (Permission denied) 

how can i get the permission to write on Download folder (this is system Download folder) without having to open an activity to save the file?

i'll try multiple solution yet(2 day of google) without success

for now as stated we target API 28 (android 9) we will later target other API since we provide the device to the client so we develop only for the API our device have.

CodePudding user response:

I've recently had to develop an app that downloads voice files to a device. While you can specify permissions in the Android manifest, you must request permissions from the user. I've done so in Java, but a conversion to Kotlin should be simple.

//method is called to check the storage permissions for this app
//ensures the app can write and read files
private void checkStoragePermissions() {
    Log.i("Permissions", "Checking Storage Permissions");

    int writePermissionCode = ContextCompat.checkSelfPermission(this, Manifest.permission.WRITE_EXTERNAL_STORAGE);//get current write permission
    int readPermissionCode = ContextCompat.checkSelfPermission(this, Manifest.permission.READ_EXTERNAL_STORAGE);//ge current read permission
    Log.i("Permissions", "Fetching Read & Write Codes: "   readPermissionCode   "/"   writePermissionCode);

    //if permissions to read and write to external storage is not granted
    if (writePermissionCode != PackageManager.PERMISSION_GRANTED || readPermissionCode != PackageManager.PERMISSION_GRANTED) {
        //request read and write permissions
        ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE}, PackageManager.PERMISSION_GRANTED);
        ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.READ_EXTERNAL_STORAGE}, PackageManager.PERMISSION_GRANTED);
        Log.i("Permissions", "Asking For Storage Permissions");

    } else {//else: if permissions to read and write is already granted
        permissionsGranted = true;//set permissions granted bool to true
    }
}

After you've done this the downloading of the files can be done in many ways. It's worth noting that files can't be downloaded to any location on an Android device. Only a specific destinations can be used.

I hope this helps to clear some of your confusion. Happy coding!

CodePudding user response:

how can i get the permission to write on Download folder (this is system Download folder) without having to open an activity to save the file?

The simplest solution is to write somewhere else, where you do not need permissions. The methods on Context that return File objects, like getFilesDir() and getExternalFilesDir(), are your primary candidates.

Beyond that, it appears that your app is pre-installed on some device ("we provide the device to the client"). If you have developed a custom firmware image, you should be able to pre-grant the permission to your app as part of that image. Or, if the device is being distributed already configured (no first-time-power-on onboarding UI), you could manually grant WRITE_EXTERNAL_STORAGE to your app or have a UI automation script do it.

If none of those are options, then you have no choice but to ask the user for permission.

  • Related