Example usage for android.content ContentResolver getPersistedUriPermissions

List of usage examples for android.content ContentResolver getPersistedUriPermissions

Introduction

In this page you can find the example usage for android.content ContentResolver getPersistedUriPermissions.

Prototype

public @NonNull List<UriPermission> getPersistedUriPermissions() 

Source Link

Document

Return list of all URI permission grants that have been persisted by the calling app.

Usage

From source file:com.commonsware.android.diceware.PassphraseFragment.java

private static boolean obtainDurablePermission(ContentResolver resolver, Uri document) {
    boolean weHaveDurablePermission = false;

    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
        int perms = Intent.FLAG_GRANT_READ_URI_PERMISSION | Intent.FLAG_GRANT_WRITE_URI_PERMISSION;

        try {/*ww  w  .  ja  va2  s .co  m*/
            resolver.takePersistableUriPermission(document, perms);

            for (UriPermission perm : resolver.getPersistedUriPermissions()) {
                if (perm.getUri().equals(document)) {
                    weHaveDurablePermission = true;
                }
            }
        } catch (SecurityException e) {
            // OK, we were not offered any persistable permissions
        }
    }

    return (weHaveDurablePermission);
}

From source file:de.cachebox_test.splash.java

@TargetApi(Build.VERSION_CODES.KITKAT)
@Override//from w  w  w .j a  va  2  s . c o m
protected void onActivityResult(int requestCode, int resultCode, Intent data) {

    if (resultCode == RESULT_OK && requestCode == Global.REQUEST_CODE_GET_WRITE_PERMISSION_ANDROID_5) {
        Uri treeUri = data.getData();

        Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
        final int takeFlags = intent.getFlags()
                & (Intent.FLAG_GRANT_READ_URI_PERMISSION | Intent.FLAG_GRANT_WRITE_URI_PERMISSION);

        // Check for the freshest data.

        // Uri workPathUri = Uri.parse(workPath);

        ContentResolver cr = getContentResolver();

        grantUriPermission(getPackageName(), treeUri, Intent.FLAG_GRANT_READ_URI_PERMISSION);
        cr.takePersistableUriPermission(treeUri, takeFlags);

        List<UriPermission> permissionlist = cr.getPersistedUriPermissions();

        LolipopworkPath = "content://com.android.externalstorage.documents/tree/B8C5-760B%3A";// treeUri.getPath();

        Thread th = new Thread(new Runnable() {

            @Override
            public void run() {
                Initial(width, height);
            }
        });
        th.start();

    }

    if (resultCode == RESULT_OK
            && requestCode == Global.REQUEST_CODE_PICK_FILE_OR_DIRECTORY_FROM_PLATFORM_CONECTOR) {
        if (resultCode == android.app.Activity.RESULT_OK && data != null) {
            // obtain the filename
            Uri fileUri = data.getData();
            if (fileUri != null) {
                String filePath = fileUri.getPath();
                if (filePath != null) {
                    if (getFolderReturnListener != null)
                        getFolderReturnListener.getFolderReturn(filePath);
                }
            }
        }
        return;

    }

}