saber javadi
saber javadi

Reputation: 542

Read and Write external storage permission isn't working

In my app i want to get user permission to read and write data on external storage, i have put permission tag in my manifest as below. But when installing app or running and where it requires to read or write to storage no permission is taken from user and reading and writing memory just doesn't work, until i manually go to setting-app-access and enable access to memory.

My manifest is this:

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

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

   <application
      android:allowBackup="true"
      android:dialogTheme="@style/CustomDialog"
      android:icon="@mipmap/myappicon"
      android:label="@string/app_name"
      android:roundIcon="@mipmap/ic_launcher_round"
      android:supportsRtl="true"
      android:theme="@style/AppTheme">
      <activity android:name=".MainActivity">
          <intent-filter>
              <action android:name="android.intent.action.MAIN" />
              <category android:name="android.intent.category.LAUNCHER" />
          </intent-filter>
      </activity>
      <activity android:name=".insertData" />
      <activity android:name=".setting" />
      <activity android:name=".dellAll" />
      <activity android:name=".reports" />
      <activity android:name=".login" />
  </application>

</manifest>

Upvotes: 24

Views: 96154

Answers (6)

Dedetok
Dedetok

Reputation: 65

for android 6 (api 23) or higher try to use app -> manifests -> AndroidManifest.xml

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

    <!-- for >= android 6 api level 23 -->
    <uses-permission-sdk-23 android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
    <uses-permission-sdk-23 android:name="android.permission.READ_EXTERNAL_STORAGE" /> 

    <application
...

Upvotes: 1

Babul
Babul

Reputation: 1224

Read and Write permission for storage and gallery usage for marshmallow or above:

  private void requestPermission() {
      if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
          requestPermissions(new String[] {
              android.Manifest.permission.WRITE_EXTERNAL_STORAGE}, REQUEST_WRITE_PERMISSION);
       } else {
           openFilePicker();
       }
  }

To check permission result use below method:

 @Override
 public void onRequestPermissionsResult(int requestCode, @NonNull String[] 
                                        permissions, @NonNull int[] grantResults) {
    super.onRequestPermissionsResult(requestCode, permissions, grantResults);

    switch (requestCode) {
        case STORAGE_PERMISSION_REQUEST_CODE:
            if (grantResults.length > 0 && permissions[0].equals(Manifest.permission.WRITE_EXTERNAL_STORAGE)) {
                // check whether storage permission granted or not.
                if (grantResults[0] == PackageManager.PERMISSION_GRANTED) {
                    // do what you want;
                }
            }
            break;
        default:
            break;
    }
}

Upvotes: 5

M&#39;aiq the Coder
M&#39;aiq the Coder

Reputation: 812

Recommended solution: User a permission library like Dexter or KotlinPermissions or RxPermissions. I have used all of them and they are quite reliable.

Else:

Write these lines in your activity, just bellow super.onCreate(...) and before setContentView(...):

super.onCreate(savedInstanceState);

    // Here, thisActivity is the current activity

    if (ContextCompat.checkSelfPermission(this, Manifest.permission.READ_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED) {
        ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.READ_EXTERNAL_STORAGE}, 1);
    }

    if (ContextCompat.checkSelfPermission(this, Manifest.permission.WRITE_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED) {
        ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE}, 1);
    }

    if (ContextCompat.checkSelfPermission(this, Manifest.permission.CAMERA) != PackageManager.PERMISSION_GRANTED) {
        ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.CAMERA}, 1);
    }
    setContentView(R.layout.activity_main);

Now it should work, it fixed the problem for me!

Upvotes: 10

ovidiur
ovidiur

Reputation: 338

Clean the damn project, rebuild and run if it still does not work....

Add this code in onCreate method in your activity. Delete the app, clean the project, build and run. Also do not forget to add the permission you want in your manifest. (For a lower API level the code is unnecessary but from 23 and up you also need this code)

private fun requestPermission() {

    val permissions = arrayOf(Manifest.permission.ACCESS_FINE_LOCATION, Manifest.permission.WRITE_EXTERNAL_STORAGE)
    this.requestPermissions(permissions, 5)
}

Upvotes: 2

maheryhaja
maheryhaja

Reputation: 1687

in android API >= 23 you need to request permission at runtime. Take a look here

Something like this

        // Here, thisActivity is the current activity
    if (ContextCompat.checkSelfPermission(thisActivity,
            Manifest.permission.WRITE_EXTERNAL_STORAGE)
            != PackageManager.PERMISSION_GRANTED) {

        // Permission is not granted
        // Should we show an explanation?
        if (ActivityCompat.shouldShowRequestPermissionRationale(thisActivity,
                Manifest.permission.WRITE_EXTERNAL_STORAGE)) {
            // Show an explanation to the user *asynchronously* -- don't block
            // this thread waiting for the user's response! After the user
            // sees the explanation, try again to request the permission.
        } else {
            // No explanation needed; request the permission
            ActivityCompat.requestPermissions(thisActivity,
                    new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE},
                    MY_PERMISSIONS_WRITE_EXTERNAL_STORAGE);

            // MY_PERMISSIONS_REQUEST_READ_CONTACTS is an
            // app-defined int constant. The callback method gets the
            // result of the request.
        }
    } else {
        // Permission has already been granted
    }

however, Ted permission library is a gread lib to avoid such boilerplate code.

//call back after permission granted
    PermissionListener permissionlistener = new PermissionListener() {
        @Override
        public void onPermissionGranted() {
            Toast.makeText(MainActivity.this, "Permission Granted", Toast.LENGTH_SHORT).show();
        }

        @Override
        public void onPermissionDenied(List<String> deniedPermissions) {
            Toast.makeText(MainActivity.this, "Permission Denied\n" + deniedPermissions.toString(), Toast.LENGTH_SHORT).show();
        }


    };

    //check all needed permissions together
    TedPermission.with(this)
            .setPermissionListener(permissionlistener)
            .setDeniedMessage("If you reject permission,you can not use this service\n\nPlease turn on permissions at [Setting] > [Permission]")
            .setPermissions(Manifest.permission.READ_EXTERNAL_STORAGE, Manifest.permission.WRITE_EXTERNAL_STORAGE)
            .check();

Upvotes: 28

Jarvis
Jarvis

Reputation: 1802

From android 24 and above you need runtime permission to access some feature of device like Microphone and camera, storage etc. check you have storage permission access using below code

  private boolean hasStoragePermission(int requestCode) {
    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}, requestCode);
            return false;
        } else {
            return true;
        }
    } else {
        return true;
    }
}

It show popup to allow for access, if its allow perform your action

 @Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
    super.onRequestPermissionsResult(requestCode, permissions, grantResults);
    if (grantResults.length > 0
            && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
        if (requestCode == FILE_ATTACHMENT)
            attachFile();

    }
}

Upvotes: 4

Related Questions