2

Hello I am deleting some items from SD card. And in lollipop first time i ask for permission and user grant permission, but now i don't want to ask every time for permission. Its very irritating and many of guys they afraid from this behavior. How can i save my permission i.e. next time i don't need to pass that Intent again and again

Intent intent = new Intent(Intent.ACTION_OPEN_DOCUMENT_TREE);

Thank you

Bhanu Sharma
  • 5,135
  • 2
  • 24
  • 49
  • check this answer it will help you : http://stackoverflow.com/questions/37725699/issues-in-writing-files-to-sd-card-in-android-api-23marshmallow/37725902#37725902 – Vickyexpert Aug 29 '16 at 05:48

3 Answers3

1

I give you some code to example. Remember that u need check permission on Android 6.0. OnRequestPermission is method that when u have permission will be do code in grantResult, if not u can ask for permission or saying message that user not give a permission. And this method save status for permission

@TargetApi(23)
public void checkStoragePermission() {
    if (Build.VERSION.SDK_INT < Build.VERSION_CODES.M) {
        if (settingsDrawerFragment != null) {
            settingsDrawerFragment.onPermissionGranted();
        }
        return;
    }
    if (this.checkSelfPermission(Manifest.permission.READ_EXTERNAL_STORAGE) != PackageManager
            .PERMISSION_GRANTED) {
        requestPermissions(new String[]{Manifest.permission.READ_EXTERNAL_STORAGE},
                REQUEST_CODE_EXTERNAL_STORAGE);
        return;
    }
    if (settingsDrawerFragment != null) {
        settingsDrawerFragment.onPermissionGranted();
    }
}

@Override
public void onRequestPermissionsResult(int requestCode, String[] permissions, int[]
        grantResults) {
    switch (requestCode) {
        case REQUEST_CODE_AUDIO_RECORD:
            if (grantResults[0] == PackageManager.PERMISSION_GRANTED) {
                startBrowseActivity();
            } else {
                Utils.showToast(this, getString(R.string.audio_permission_denied));
            }
            break;
        case REQUEST_CODE_EXTERNAL_STORAGE:
            if (grantResults[0] == PackageManager.PERMISSION_GRANTED) {
                if (settingsDrawerFragment != null) {
                    settingsDrawerFragment.onPermissionGranted();
                }
            } else {
                if (settingsDrawerFragment != null) {
                    closeSettingsDrawer();
                }
                Utils.showToast(this, getString(R.string.storage_permission_denied));
            }
            break;
        default:
            super.onRequestPermissionsResult(requestCode, permissions, grantResults);
            break;
    }
}
Genehme
  • 69
  • 9
  • have u know about this Intent intent = new Intent(Intent.ACTION_OPEN_DOCUMENT_TREE); i want this permission to avoid – Bhanu Sharma Aug 29 '16 at 05:43
  • check this http://stackoverflow.com/questions/26744842/how-to-use-the-new-sd-card-access-api-presented-for-lollipop also if u have onrequestPermissionsResult method- this save ur permission on application – Genehme Aug 29 '16 at 05:46
  • Intent intent = new Intent(Intent.ACTION_OPEN_DOCUMENT_TREE); intent.putExtra(Intent.EXTRA_ALLOW_MULTIPLE, true); intent.addFlags(Intent.FLAG_GRANT_PERSISTABLE_URI_PERMISSION); intent.putExtra(Intent.EXTRA_LOCAL_ONLY, true); intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION); intent.addFlags(Intent.FLAG_GRANT_WRITE_URI_PERMISSION); startActivityForResult(intent, 42); builder.dismiss(); – Bhanu Sharma Aug 29 '16 at 06:14
1

You can use the following code to persist permission

 getContentResolver().takePersistableUriPermission(treeUri,
            Intent.FLAG_GRANT_READ_URI_PERMISSION |
            Intent.FLAG_GRANT_WRITE_URI_PERMISSION);

Also once you persist permission save the uri in a sharedPreference so that you can delete files later without confirmation.

JAAD
  • 12,349
  • 7
  • 36
  • 57
  • i save that uri in sharedPreference but after phone restart again this prob occur – Bhanu Sharma Aug 29 '16 at 05:54
  • are you sure that you are using the above code to persist permission – JAAD Aug 29 '16 at 05:54
  • Intent intent = new Intent(Intent.ACTION_OPEN_DOCUMENT_TREE); intent.putExtra(Intent.EXTRA_ALLOW_MULTIPLE, true); intent.addFlags(Intent.FLAG_GRANT_PERSISTABLE_URI_PERMISSION); intent.putExtra(Intent.EXTRA_LOCAL_ONLY, true); intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION); intent.addFlags(Intent.FLAG_GRANT_WRITE_URI_PERMISSION); startActivityForResult(intent, 42); builder.dismiss(); – Bhanu Sharma Aug 29 '16 at 06:15
  • in onactivity result where you get the uri – JAAD Aug 29 '16 at 06:17
  • and in onresult and get this treeUri = data.getData(); and this uri i save for next deletion – Bhanu Sharma Aug 29 '16 at 06:17
  • onActivityResult get this treeUri = data.getData(); – Bhanu Sharma Aug 29 '16 at 06:18
  • localUri2 = DocumentsContract.buildDocumentUriUsingTree(treeUri, DocumentsContract.getTreeDocumentId(treeUri)); – Bhanu Sharma Aug 29 '16 at 06:22
  • arrayOfString = MediaWrapper.get(mediaCount).getFilePath().split("/"); localObject = DocumentFile.fromTreeUri(MediaContentActivity.this, localUri2); int x = arrayOfString.length; – Bhanu Sharma Aug 29 '16 at 06:23
  • for (int jj = 0; jj < x; jj++) { DocumentFile localDocumentFile = null; try{ localDocumentFile = ((DocumentFile)localObject).findFile(arrayOfString[jj]); if (localDocumentFile != null) { localObject = localDocumentFile; } } catch(Exception e) { e.printStackTrace(); } finally{ localDocumentFile = null; } } – Bhanu Sharma Aug 29 '16 at 06:23
  • if ((((DocumentFile)localObject).isFile()) && (((DocumentFile)localObject).canWrite())) { boolean isFileDelete = ((DocumentFile)localObject).delete();} – Bhanu Sharma Aug 29 '16 at 06:23
  • and i found android.permission.MANAGE_DOCUMENTS error here from last URI – Bhanu Sharma Aug 29 '16 at 07:02
  • r u there i write all my lines above? – Bhanu Sharma Aug 29 '16 at 08:34
0

You can launch your intent by adding falgs.

 Intent intent = new Intent(Intent.ACTION_OPEN_DOCUMENT_TREE);
 intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION |
                        Intent.FLAG_GRANT_WRITE_URI_PERMISSION);
 startActivity(intent);
Sohail Zahid
  • 8,099
  • 2
  • 25
  • 41