Search code examples
androidandroid-download-managerandroid-11

Using download manager in andorid 11 and the impact of scoped storage


What I am doing

  • I am trying to use the Download Manager to download a file to the device internal storage
  • Internal storage referring to the location /storage/emulated/0/Android/data/com.example.code/files/Download/Devrath/test.db
  • Now I am using an emulator below is the code which is working and successfully able to download the file

Questions

  1. Will the scoped storage affect this in the future?
  2. Or Since Download manager is a system service, will it continue to work

Manifest

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.code">

    <uses-permission android:name="android.permission.INTERNET"/>
    <uses-permission android:name="android.permission.DOWNLOAD_WITHOUT_NOTIFICATION"/>

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:usesCleartextTraffic="true"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/Theme.Code">
        <activity android:name=".MainActivityTwo">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>

gradle

compileSdkVersion 31
    buildToolsVersion "30.0.3"

    defaultConfig {
        applicationId "com.example.code"
        minSdkVersion 16
        targetSdkVersion 31
        versionCode 1
        versionName "1.0"

        testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
    }

MainActivityTwo

public class MainActivityTwo extends AppCompatActivity {

    private static final int REQUEST_CODE = 100;
    //public static final String imageURL = "http://www.tutorialspoint.com/java/java_tutorial.pdf";
    public static final String imageURL = "http://speedtest.ftp.otenet.gr/files/test10Mb.db";
    // String imageName = "java_tutorial.pdf";

    String filePath = "";
    String imageName = "";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        filePath = getExternalFilesDir(Environment.DIRECTORY_DOWNLOADS).toString()
                .concat(File.separator)
                .concat("Devrath").concat(File.separator);

        imageName = "test.db";

        // storage runtime permission
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
            if (checkSelfPermission(Manifest.permission.WRITE_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED) {
                requestPermissions(new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE}, REQUEST_CODE);
            }
        }

        Button btnDownloadImage = findViewById(R.id.initiateDownloadId);
        Button chkIfFileExistsId = findViewById(R.id.chkIfFileExistsId);
        btnDownloadImage.setOnClickListener(v -> downloadImage(imageURL, imageName));
        chkIfFileExistsId.setOnClickListener(v -> checkIfFileExists());

    }

    public void downloadImage(String url, String outputFileName) {
        DownloadManager.Request request = new DownloadManager.Request(Uri.parse(url));
        request.setTitle(imageName);
        request.setDescription("Downloading " + imageName);
        request.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED);
        request.allowScanningByMediaScanner();
        //request.setDestinationInExternalPublicDir(Environment.DIRECTORY_DOWNLOADS, outputFileName); // ---> Working
        //request.setDestinationInExternalFilesDir(this, Environment.getExternalStorageDirectory().toString() + File.separator, outputFileName);
        request.setDestinationInExternalFilesDir(this,filePath, outputFileName);
        DownloadManager manager = (DownloadManager) getSystemService(Context.DOWNLOAD_SERVICE);
        manager.enqueue(request);
    }

    private void checkIfFileExists() {

        //-> /storage/emulated/0/Android/data/com.example.code/files/Download/Devrath/test.db
        File mFile = new File(filePath.concat(imageName));
        if(mFile.exists()){
            Toast.makeText(this,"File exists",Toast.LENGTH_LONG).show();
        }else{
            Toast.makeText(this,"File does not exists",Toast.LENGTH_LONG).show();
        }
    }
}

Solution

    • I ended up using the app's internal storage... I have created a GitHub project.
    • Posting here so it will help someone.
    • There is a Complete working solution I made

    GIT-HUB - Link