0

I am a newbie.

My app has storage permissions which are running well for all api below 5.

(These are used for the Browse Button to choose files or folders and then perform the specified action)

Permissions are (in AndroidManifest.xml file):

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

I want to know how to give permission for "storage" at runtime for API above 5.1.

What I have done till now?: I have searched for it on the net, but there is not any complete answer, in detail, for Storage permission.

All answers will be appreciated.

And this question is not a duplicate of Run-time permission of Marshmallow, as that is about: How to handle "Never Ask Again" but I asked this question as I don't get any detailed complete description for a newbie for Strorage permissions.

Community
  • 1
  • 1
Bati
  • 529
  • 3
  • 6
  • 15
  • Possible duplicate of [Run-time permission of Marshmallow](http://stackoverflow.com/questions/34717383/run-time-permission-of-marshmallow) – Mr X Jun 27 '16 at 12:08
  • @Mr X, Sir, this question is not a duplicate, see explanation above, in the question. – Bati Jun 27 '16 at 12:21

3 Answers3

1

You can give your permission like this:

public  boolean isStoragePermissionGranted() {
    if (Build.VERSION.SDK_INT >= 23) {
        if (checkSelfPermission(android.Manifest.permission.WRITE_EXTERNAL_STORAGE)
                == PackageManager.PERMISSION_GRANTED) {
            Log.v(TAG,"Permission is granted");
            return true;
        } else {

            Log.v(TAG,"Permission is revoked");
            ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE}, 1);
            return false;
        }
    }
    else { //permission is automatically granted on sdk<23 upon installation
        Log.v(TAG,"Permission is granted");
        return true;
    }


}

To check the permission callback use this:

@Override
public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
    super.onRequestPermissionsResult(requestCode, permissions, grantResults);
    if(grantResults[0]== PackageManager.PERMISSION_GRANTED){
        Log.d(TAG,"Permission: "+permissions[0]+ "was "+grantResults[0]);
        //resume tasks needing this permission
    }
}
Lips_coder
  • 686
  • 1
  • 5
  • 17
  • @CrazyAndriod: I have a MainActivity.java and some other fragments (used with Navigation Drawer), can you please tell, where to put this code, I want to take the essential permission during first time and then checking only. – Bati Jun 27 '16 at 12:57
  • You can write the method in your Activity or Fragment or you can create an AppUtil class,you can easily make the above method static.@Bati – Lips_coder Jun 27 '16 at 13:00
1

Try using if else condition to check out what version any particular mobile has

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            // your code
        }
Deep Gosalia
  • 187
  • 1
  • 1
  • 6
0

So are you talking about adding Rumtime permissions for Andorid 6.0+?

If so, this page shows you how to implement them.

See this StackOverflow post for more info.

Community
  • 1
  • 1
biddulph.r
  • 5,226
  • 3
  • 32
  • 47