Java Code Examples for android.content.Intent#FLAG_GRANT_READ_URI_PERMISSION

The following examples show how to use android.content.Intent#FLAG_GRANT_READ_URI_PERMISSION . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: MainActivity.java    From OneText_For_Android with GNU Lesser General Public License v3.0 6 votes vote down vote up
@Override
public void onActivityResult(int requestCode, int resultCode, Intent resultData) {
    super.onActivityResult(requestCode, resultCode, resultData);
    if (requestCode == 233 && resultCode == Activity.RESULT_OK) {
        Uri uriTree = resultData.getData();
        if (uriTree != null) {
            final int takeFlags = getIntent().getFlags() & (Intent.FLAG_GRANT_READ_URI_PERMISSION | Intent.FLAG_GRANT_WRITE_URI_PERMISSION);
            getContentResolver().takePersistableUriPermission(uriTree, takeFlags);
            editor.putString("pic_uri_tree", uriTree.toString());
            editor.apply();
            // 创建所选目录的DocumentFile,可以使用它进行文件操作
            DocumentFile root = DocumentFile.fromTreeUri(this, uriTree);
            // 比如使用它创建文件夹
            //DocumentFile[] rootList = root.listFiles();
            shotOneTextViaSAF(root);
        }
    }
}
 
Example 2
Source File: ContextImpl.java    From android_9.0.0_r45 with Apache License 2.0 6 votes vote down vote up
@Override
public int checkUriPermission(Uri uri, String readPermission,
        String writePermission, int pid, int uid, int modeFlags) {
    if (DEBUG) {
        Log.i("foo", "checkUriPermission: uri=" + uri + "readPermission="
                + readPermission + " writePermission=" + writePermission
                + " pid=" + pid + " uid=" + uid + " mode" + modeFlags);
    }
    if ((modeFlags&Intent.FLAG_GRANT_READ_URI_PERMISSION) != 0) {
        if (readPermission == null
                || checkPermission(readPermission, pid, uid)
                == PackageManager.PERMISSION_GRANTED) {
            return PackageManager.PERMISSION_GRANTED;
        }
    }
    if ((modeFlags&Intent.FLAG_GRANT_WRITE_URI_PERMISSION) != 0) {
        if (writePermission == null
                || checkPermission(writePermission, pid, uid)
                == PackageManager.PERMISSION_GRANTED) {
            return PackageManager.PERMISSION_GRANTED;
        }
    }
    return uri != null ? checkUriPermission(uri, pid, uid, modeFlags)
            : PackageManager.PERMISSION_DENIED;
}
 
Example 3
Source File: ContextImpl.java    From AndroidComponentPlugin with Apache License 2.0 6 votes vote down vote up
private String uriModeFlagToString(int uriModeFlags) {
    StringBuilder builder = new StringBuilder();
    if ((uriModeFlags & Intent.FLAG_GRANT_READ_URI_PERMISSION) != 0) {
        builder.append("read and ");
    }
    if ((uriModeFlags & Intent.FLAG_GRANT_WRITE_URI_PERMISSION) != 0) {
        builder.append("write and ");
    }
    if ((uriModeFlags & Intent.FLAG_GRANT_PERSISTABLE_URI_PERMISSION) != 0) {
        builder.append("persistable and ");
    }
    if ((uriModeFlags & Intent.FLAG_GRANT_PREFIX_URI_PERMISSION) != 0) {
        builder.append("prefix and ");
    }

    if (builder.length() > 5) {
        builder.setLength(builder.length() - 5);
        return builder.toString();
    } else {
        throw new IllegalArgumentException("Unknown permission mode flags: " + uriModeFlags);
    }
}
 
Example 4
Source File: FileChooser.java    From keemob with MIT License 6 votes vote down vote up
@Override
public void onActivityResult(int requestCode, int resultCode, Intent intent) {

    if (resultCode != Activity.RESULT_OK) {
        return;
    }

    if (requestCode == CODE_CHOOSE_FILE) {
        Uri uri = intent.getData();

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
            final int takeFlags = intent.getFlags()
                    & (Intent.FLAG_GRANT_READ_URI_PERMISSION
                    | Intent.FLAG_GRANT_WRITE_URI_PERMISSION);
            cordova.getActivity().getContentResolver().takePersistableUriPermission(uri, takeFlags);
        }

        callbackContext.success(asFile(uri));
        callbackContext = null;
    }

    super.onActivityResult(requestCode, resultCode, intent);
}
 
Example 5
Source File: ContextImpl.java    From AndroidComponentPlugin with Apache License 2.0 6 votes vote down vote up
@Override
public int checkUriPermission(Uri uri, String readPermission,
        String writePermission, int pid, int uid, int modeFlags) {
    if (DEBUG) {
        Log.i("foo", "checkUriPermission: uri=" + uri + "readPermission="
                + readPermission + " writePermission=" + writePermission
                + " pid=" + pid + " uid=" + uid + " mode" + modeFlags);
    }
    if ((modeFlags&Intent.FLAG_GRANT_READ_URI_PERMISSION) != 0) {
        if (readPermission == null
                || checkPermission(readPermission, pid, uid)
                == PackageManager.PERMISSION_GRANTED) {
            return PackageManager.PERMISSION_GRANTED;
        }
    }
    if ((modeFlags&Intent.FLAG_GRANT_WRITE_URI_PERMISSION) != 0) {
        if (writePermission == null
                || checkPermission(writePermission, pid, uid)
                == PackageManager.PERMISSION_GRANTED) {
            return PackageManager.PERMISSION_GRANTED;
        }
    }
    return uri != null ? checkUriPermission(uri, pid, uid, modeFlags)
            : PackageManager.PERMISSION_DENIED;
}
 
Example 6
Source File: UriPermission.java    From android_9.0.0_r45 with Apache License 2.0 6 votes vote down vote up
boolean releasePersistableModes(int modeFlags) {
    modeFlags &= (Intent.FLAG_GRANT_READ_URI_PERMISSION
            | Intent.FLAG_GRANT_WRITE_URI_PERMISSION);

    final int before = persistedModeFlags;

    persistableModeFlags &= ~modeFlags;
    persistedModeFlags &= ~modeFlags;

    if (persistedModeFlags == 0) {
        persistedCreateTime = INVALID_TIME;
    }

    updateModeFlags();
    return persistedModeFlags != before;
}
 
Example 7
Source File: ShareUtil.java    From openlauncher with Apache License 2.0 6 votes vote down vote up
/**
 * Request edit of image (by image editor/viewer - for example to crop image)
 *
 * @param file File that should be edited
 */
public void requestPictureEdit(final File file) {
    Uri uri = getUriByFileProviderAuthority(file);
    int flags = Intent.FLAG_GRANT_WRITE_URI_PERMISSION | Intent.FLAG_GRANT_READ_URI_PERMISSION;

    Intent intent = new Intent(Intent.ACTION_EDIT);
    intent.setDataAndType(uri, "image/*");
    intent.addFlags(flags);
    intent.putExtra(MediaStore.EXTRA_OUTPUT, uri);
    intent.putExtra(EXTRA_FILEPATH, file.getAbsolutePath());

    for (ResolveInfo resolveInfo : _context.getPackageManager().queryIntentActivities(intent, PackageManager.MATCH_DEFAULT_ONLY)) {
        String packageName = resolveInfo.activityInfo.packageName;
        _context.grantUriPermission(packageName, uri, flags);
    }
    _context.startActivity(Intent.createChooser(intent, null));
}
 
Example 8
Source File: ContextImpl.java    From AndroidComponentPlugin with Apache License 2.0 6 votes vote down vote up
@Override
public int checkUriPermission(Uri uri, String readPermission,
        String writePermission, int pid, int uid, int modeFlags) {
    if (DEBUG) {
        Log.i("foo", "checkUriPermission: uri=" + uri + "readPermission="
                + readPermission + " writePermission=" + writePermission
                + " pid=" + pid + " uid=" + uid + " mode" + modeFlags);
    }
    if ((modeFlags&Intent.FLAG_GRANT_READ_URI_PERMISSION) != 0) {
        if (readPermission == null
                || checkPermission(readPermission, pid, uid)
                == PackageManager.PERMISSION_GRANTED) {
            return PackageManager.PERMISSION_GRANTED;
        }
    }
    if ((modeFlags&Intent.FLAG_GRANT_WRITE_URI_PERMISSION) != 0) {
        if (writePermission == null
                || checkPermission(writePermission, pid, uid)
                == PackageManager.PERMISSION_GRANTED) {
            return PackageManager.PERMISSION_GRANTED;
        }
    }
    return uri != null ? checkUriPermission(uri, pid, uid, modeFlags)
            : PackageManager.PERMISSION_DENIED;
}
 
Example 9
Source File: ContextImpl.java    From AndroidComponentPlugin with Apache License 2.0 6 votes vote down vote up
@Override
public int checkUriPermission(Uri uri, String readPermission,
        String writePermission, int pid, int uid, int modeFlags) {
    if (DEBUG) {
        Log.i("foo", "checkUriPermission: uri=" + uri + "readPermission="
                + readPermission + " writePermission=" + writePermission
                + " pid=" + pid + " uid=" + uid + " mode" + modeFlags);
    }
    if ((modeFlags&Intent.FLAG_GRANT_READ_URI_PERMISSION) != 0) {
        if (readPermission == null
                || checkPermission(readPermission, pid, uid)
                == PackageManager.PERMISSION_GRANTED) {
            return PackageManager.PERMISSION_GRANTED;
        }
    }
    if ((modeFlags&Intent.FLAG_GRANT_WRITE_URI_PERMISSION) != 0) {
        if (writePermission == null
                || checkPermission(writePermission, pid, uid)
                == PackageManager.PERMISSION_GRANTED) {
            return PackageManager.PERMISSION_GRANTED;
        }
    }
    return uri != null ? checkUriPermission(uri, pid, uid, modeFlags)
            : PackageManager.PERMISSION_DENIED;
}
 
Example 10
Source File: MainActivity.java    From ml with Apache License 2.0 6 votes vote down vote up
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    Logger.debug("requestCode = %d, resultCode = %d, data = %s",
            requestCode,
            resultCode,
            data);
    if (requestCode == REQUEST_PICK_IMAGE
            && resultCode == RESULT_OK) {
        Uri pickedImageUri = data.getData();
        Logger.debug("picked: %s", pickedImageUri);

        if (pickedImageUri != null) {
            if(Build.VERSION.SDK_INT >= 19){
                final int takeFlags = data.getFlags() & Intent.FLAG_GRANT_READ_URI_PERMISSION;
                getContentResolver()
                        .takePersistableUriPermission(pickedImageUri, takeFlags);
            }

            analyzeImage(pickedImageUri);
        }
    } else {
        super.onActivityResult(requestCode, resultCode, data);
    }
}
 
Example 11
Source File: MainActivity.java    From Android-BitherCompress with Apache License 2.0 5 votes vote down vote up
@SuppressWarnings("ResourceType")
@TargetApi(Build.VERSION_CODES.KITKAT)
public static Uri ensureUriPermission(Context context, Intent intent) {
    Uri uri = intent.getData();
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
        final int takeFlags = intent.getFlags() & Intent.FLAG_GRANT_READ_URI_PERMISSION;
        context.getContentResolver().takePersistableUriPermission(uri, takeFlags);
    }
    return uri;
}
 
Example 12
Source File: FileProvider7.java    From Android-BLE with Apache License 2.0 5 votes vote down vote up
public static void grantPermissions(Context context, Intent intent, Uri uri, boolean writeAble) {

        int flag = Intent.FLAG_GRANT_READ_URI_PERMISSION;
        if (writeAble) {
            flag |= Intent.FLAG_GRANT_WRITE_URI_PERMISSION;
        }
        intent.addFlags(flag);
        List<ResolveInfo> resInfoList = context.getPackageManager()
                .queryIntentActivities(intent, PackageManager.MATCH_DEFAULT_ONLY);
        for (ResolveInfo resolveInfo : resInfoList) {
            String packageName = resolveInfo.activityInfo.packageName;
            context.grantUriPermission(packageName, uri, flag);
        }
    }
 
Example 13
Source File: ContextImpl.java    From AndroidComponentPlugin with Apache License 2.0 5 votes vote down vote up
private String uriModeFlagToString(int uriModeFlags) {
    switch (uriModeFlags) {
        case Intent.FLAG_GRANT_READ_URI_PERMISSION |
                Intent.FLAG_GRANT_WRITE_URI_PERMISSION:
            return "read and write";
        case Intent.FLAG_GRANT_READ_URI_PERMISSION:
            return "read";
        case Intent.FLAG_GRANT_WRITE_URI_PERMISSION:
            return "write";
    }
    throw new IllegalArgumentException(
            "Unknown permission mode flags: " + uriModeFlags);
}
 
Example 14
Source File: PreferencesFragment.java    From Aegis with GNU General Public License v3.0 5 votes vote down vote up
private void onSelectBackupsLocationResult(int resultCode, Intent data) {
    Uri uri = data.getData();
    if (resultCode != Activity.RESULT_OK || uri == null) {
        return;
    }

    int flags = data.getFlags() & (Intent.FLAG_GRANT_READ_URI_PERMISSION | Intent.FLAG_GRANT_WRITE_URI_PERMISSION);
    getContext().getContentResolver().takePersistableUriPermission(data.getData(), flags);

    _prefs.setBackupsLocation(uri);
    _prefs.setIsBackupsEnabled(true);
    _prefs.setBackupsError(null);
    _backupsLocationPreference.setSummary(String.format("%s: %s", getString(R.string.pref_backups_location_summary), Uri.decode(uri.toString())));
    updateBackupPreference();
}
 
Example 15
Source File: UriPermission.java    From android_9.0.0_r45 with Apache License 2.0 5 votes vote down vote up
private void addReadOwner(UriPermissionOwner owner) {
    if (mReadOwners == null) {
        mReadOwners = Sets.newArraySet();
        ownedModeFlags |= Intent.FLAG_GRANT_READ_URI_PERMISSION;
        updateModeFlags();
    }
    if (mReadOwners.add(owner)) {
        owner.addReadPermission(this);
    }
}
 
Example 16
Source File: ContextImpl.java    From AndroidComponentPlugin with Apache License 2.0 5 votes vote down vote up
private String uriModeFlagToString(int uriModeFlags) {
    switch (uriModeFlags) {
        case Intent.FLAG_GRANT_READ_URI_PERMISSION |
                Intent.FLAG_GRANT_WRITE_URI_PERMISSION:
            return "read and write";
        case Intent.FLAG_GRANT_READ_URI_PERMISSION:
            return "read";
        case Intent.FLAG_GRANT_WRITE_URI_PERMISSION:
            return "write";
    }
    throw new IllegalArgumentException(
            "Unknown permission mode flags: " + uriModeFlags);
}
 
Example 17
Source File: MainActivity.java    From Ucount with GNU General Public License v3.0 5 votes vote down vote up
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    switch (requestCode) {
        case SELECT_PIC4MAIN:
            if (data == null) return;
            // 用户从图库选择图片后会返回所选图片的Uri
            Uri uri1 = data.getData();
            this.headerImg.setImageURI(uri1);
            saveImageUri(SELECT_PIC4MAIN, uri1);

            // 获取永久访问图片URI的权限
            int takeFlags = data.getFlags();
            takeFlags &=(Intent.FLAG_GRANT_READ_URI_PERMISSION
                    | Intent.FLAG_GRANT_WRITE_URI_PERMISSION);
            getContentResolver().takePersistableUriPermission(uri1, takeFlags);
            break;

        case SELECT_PIC4DRAWER:
            if (data == null) return;
            // 用户从图库选择图片后会返回所选图片的Uri
            Uri uri2 = data.getData();
            this.drawerBanner.setImageURI(uri2);
            saveImageUri(SELECT_PIC4DRAWER, uri2);

            // 获取永久访问图片URI的权限
            int takeFlags2 = data.getFlags();
            takeFlags2 &=(Intent.FLAG_GRANT_READ_URI_PERMISSION
                    | Intent.FLAG_GRANT_WRITE_URI_PERMISSION);
            getContentResolver().takePersistableUriPermission(uri2, takeFlags2);
            break;
    }
}
 
Example 18
Source File: FileProvider.java    From Dashchan with Apache License 2.0 4 votes vote down vote up
public static int getIntentFlags() {
	return C.API_NOUGAT ? Intent.FLAG_GRANT_READ_URI_PERMISSION : 0;
}
 
Example 19
Source File: MediaSource.java    From belvedere with Apache License 2.0 4 votes vote down vote up
/**
 * Internal helper method to create an {@link MediaIntent} for opening
 * an installed camera app.
 *
 * @param context A valid application {@link Context}
 * @return An {@link MediaIntent} or null if this action isn't supported by
 *      the system.
 */
private Pair<MediaIntent, MediaResult> pickImageFromCameraInternal(Context context, int requestCode){

    final File imagePath = storage.getFileForCamera(context);

    if (imagePath == null){
        L.w(Belvedere.LOG_TAG, "Camera Intent: Image path is null. There's something wrong with the storage.");
        return null;
    }

    final Uri uriForFile = storage.getFileProviderUri(context, imagePath);

    if (uriForFile == null) {
        L.w(Belvedere.LOG_TAG, "Camera Intent: Uri to file is null. There's something wrong with the storage or FileProvider configuration.");
        return null;
    }

    L.d(Belvedere.LOG_TAG, String.format(Locale.US, "Camera Intent: Request Id: %s - File: %s - Uri: %s", requestCode, imagePath, uriForFile));

    final Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
    intent.putExtra(MediaStore.EXTRA_OUTPUT, uriForFile);
    int permission = Intent.FLAG_GRANT_WRITE_URI_PERMISSION | Intent.FLAG_GRANT_READ_URI_PERMISSION;
    storage.grantPermissionsForUri(context, intent, uriForFile, permission);

    /*
        https://code.google.com/p/android/issues/detail?id=188073&q=label%3APriority-Medium&colspec=ID%20Type%20Status%20Owner%20Summary%20Stars&start=100
     */
    final boolean cameraPermissionInManifestButNoGranted =
            PermissionUtil.hasPermissionInManifest(context, Manifest.permission.CAMERA) &&
            !PermissionUtil.isPermissionGranted(context, Manifest.permission.CAMERA);

    final MediaResult r = Storage.getMediaResultForUri(context, uriForFile);
    final MediaResult belvedereResult = new MediaResult(imagePath, uriForFile, uriForFile, imagePath.getName(), r.getMimeType(), r.getSize(), r.getWidth(), r.getHeight());
    final MediaIntent mediaIntent = new MediaIntent(
            requestCode,
            intent,
            cameraPermissionInManifestButNoGranted ? Manifest.permission.CAMERA : null,
            true,
            MediaIntent.TARGET_CAMERA
    );

    return new Pair<>(mediaIntent, belvedereResult);
}
 
Example 20
Source File: Belvedere.java    From belvedere with Apache License 2.0 2 votes vote down vote up
/**
 * Revoke {@link Intent#FLAG_GRANT_READ_URI_PERMISSION} and {@link Intent#FLAG_GRANT_WRITE_URI_PERMISSION} that were
 * previously granted by {@link #grantPermissionsForUri(Intent, Uri)}.
 *
 * @param uri An {@link Uri}
 */
public void revokePermissionsForUri(@NonNull Uri uri) {
    L.d(LOG_TAG, String.format(Locale.US, "Revoke Permission - Uri: %s", uri));
    int permissions = Intent.FLAG_GRANT_READ_URI_PERMISSION | Intent.FLAG_GRANT_WRITE_URI_PERMISSION;
    storage.revokePermissionsFromUri(context, uri, permissions);
}