Updated 22 April 2017
How we get Read and Write Storage permission?
1 2 |
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /> <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" /> |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 |
public boolean isReadStoragePermissionGranted() { if (Build.VERSION.SDK_INT >= 23) { if (checkSelfPermission(Manifest.permission.READ_EXTERNAL_STORAGE) == PackageManager.PERMISSION_GRANTED) { Log.v(TAG,"Permission is granted1"); return true; } else { Log.v(TAG,"Permission is revoked1"); ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.READ_EXTERNAL_STORAGE}, 3); return false; } } else { //permission is automatically granted on sdk<23 upon installation Log.v(TAG,"Permission is granted1"); return true; } } public boolean isWriteStoragePermissionGranted() { if (Build.VERSION.SDK_INT >= 23) { if (checkSelfPermission(android.Manifest.permission.WRITE_EXTERNAL_STORAGE) == PackageManager.PERMISSION_GRANTED) { Log.v(TAG,"Permission is granted2"); return true; } else { Log.v(TAG,"Permission is revoked2"); ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE}, 2); return false; } } else { //permission is automatically granted on sdk<23 upon installation Log.v(TAG,"Permission is granted2"); return true; } } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 |
@Override public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) { super.onRequestPermissionsResult(requestCode, permissions, grantResults); switch (requestCode) { case 2: Log.d(TAG, "External storage2"); if(grantResults[0]== PackageManager.PERMISSION_GRANTED){ Log.v(TAG,"Permission: "+permissions[0]+ "was "+grantResults[0]); //resume tasks needing this permission downloadPdfFile(); }else{ progress.dismiss(); } break; case 3: Log.d(TAG, "External storage1"); if(grantResults[0]== PackageManager.PERMISSION_GRANTED){ Log.v(TAG,"Permission: "+permissions[0]+ "was "+grantResults[0]); //resume tasks needing this permission SharePdfFile(); }else{ progress.dismiss(); } break; } } |
References: StackOverflow and android developer site.
If you have more details or questions, you can reply to the received confirmation email.
Back to Home
this is madiha iqbal
can you please tell me that in which activity i have to write this code?
menifest’s activity or main activity