Write External Storage Permission is always denied

Viewed 2411

I'm using Download Manager to Download Image in my android app.

To obtain the "WRITE_EXTERNAL_STORAGE" permission I'm suing Dexter library.

        Dexter.withContext(getApplicationContext())
            .withPermission(Manifest.permission.WRITE_EXTERNAL_STORAGE)
            .withListener(new PermissionListener() {
                @Override
                public void onPermissionGranted(PermissionGrantedResponse permissionGrantedResponse) {
                    Toast.makeText(LiveTaskActivity.this, "Granted", Toast.LENGTH_SHORT).show();
                }

                @Override
                public void onPermissionDenied(PermissionDeniedResponse permissionDeniedResponse) {
                    Toast.makeText(LiveTaskActivity.this, "Denied", Toast.LENGTH_SHORT).show();
                }

                @Override
                public void onPermissionRationaleShouldBeShown(PermissionRequest permissionRequest, PermissionToken permissionToken) {

                }
            }).check();

But always I get permission "Denied" even I manually accept the permission to grant the permission from the app. I also tried manually implementing the permission request code.

        if (ActivityCompat.checkSelfPermission(this, Manifest.permission.READ_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED
            || ActivityCompat.checkSelfPermission(this, Manifest.permission.WRITE_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED) {

        ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.READ_EXTERNAL_STORAGE, Manifest.permission.WRITE_EXTERNAL_STORAGE}, 1);
    } else {
        download();
    }

Still, I can't get the permission. Here is the Manifest Code:

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

    <application
      android:requestLegacyExternalStorage="true"

Here is my buil.gradle(app) code:

android {
compileSdkVersion 30
buildToolsVersion "30.0.3"

defaultConfig {
    applicationId "app.gigg.me.app"
    minSdkVersion 22
    targetSdkVersion 30
    versionCode 2
    versionName '1.0.1'

Please help me to solve this problem.

**Update Here is my download code

    private void startDownload() {
    Toast.makeText(this, "Downloading File...", Toast.LENGTH_SHORT).show();
    fileName = link.substring(link.lastIndexOf("/") + 1) + ".png";
    File file = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS).getPath(), fileName);
    DownloadManager.Request request = new DownloadManager.Request(Uri.parse(link))
            .setTitle(fileName)
            .setDescription("Downloading")
            .setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED)
            .setDestinationUri(Uri.fromFile(file))
            .setMimeType("image/*")
            .setAllowedOverMetered(true)
            .setAllowedOverRoaming(true);
    download_id = downloadManager.enqueue(request);
}
1 Answers
Related