Java Code Examples for android.content.Intent.FLAG_GRANT_READ_URI_PERMISSION

The following are Jave code examples for showing how to use FLAG_GRANT_READ_URI_PERMISSION of the android.content.Intent class. You can vote up the examples you like. Your votes will be used in our system to get more good examples.
+ Save this method
Example 1
Project: Ucount   File: MainActivity.java   View Source Code Vote up 8 votes
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    switch (requestCode) {
        case SELECT_GALLERY_PIC:
            if (data == null) return;
            // 用户从图库选择图片后会返回所选图片的Uri
            Uri uri = data.getData();
            this.headerImg.setImageURI(uri);
            saveImageUri(uri);

            // 获取永久访问图片URI的权限
            int takeFlags = data.getFlags();
            takeFlags &=(Intent.FLAG_GRANT_READ_URI_PERMISSION
                    | Intent.FLAG_GRANT_WRITE_URI_PERMISSION);
            getContentResolver().takePersistableUriPermission(uri, takeFlags);
            break;
    }
}
 
Example 2
Project: easyfilemanager   File: DocumentsProvider.java   View Source Code Vote up 8 votes
private static int getCallingOrSelfUriPermissionModeFlags(Context context, Uri uri) {
    // TODO: move this to a direct AMS call
    int modeFlags = 0;
    if (context.checkCallingOrSelfUriPermission(uri, Intent.FLAG_GRANT_READ_URI_PERMISSION)
            == PackageManager.PERMISSION_GRANTED) {
        modeFlags |= Intent.FLAG_GRANT_READ_URI_PERMISSION;
    }
    if (context.checkCallingOrSelfUriPermission(uri, Intent.FLAG_GRANT_WRITE_URI_PERMISSION)
            == PackageManager.PERMISSION_GRANTED) {
        modeFlags |= Intent.FLAG_GRANT_WRITE_URI_PERMISSION;
    }
    if (context.checkCallingOrSelfUriPermission(uri, Intent.FLAG_GRANT_READ_URI_PERMISSION
            | Intent.FLAG_GRANT_PERSISTABLE_URI_PERMISSION)
            == PackageManager.PERMISSION_GRANTED) {
        modeFlags |= Intent.FLAG_GRANT_PERSISTABLE_URI_PERMISSION;
    }
    return modeFlags;
}
 
Example 3
Project: FireFiles   File: DocumentsProvider.java   View Source Code Vote up 8 votes
private static int getCallingOrSelfUriPermissionModeFlags(Context context, Uri uri) {
    // TODO: move this to a direct AMS call
    int modeFlags = 0;
    if (context.checkCallingOrSelfUriPermission(uri, Intent.FLAG_GRANT_READ_URI_PERMISSION)
            == PackageManager.PERMISSION_GRANTED) {
        modeFlags |= Intent.FLAG_GRANT_READ_URI_PERMISSION;
    }
    if (context.checkCallingOrSelfUriPermission(uri, Intent.FLAG_GRANT_WRITE_URI_PERMISSION)
            == PackageManager.PERMISSION_GRANTED) {
        modeFlags |= Intent.FLAG_GRANT_WRITE_URI_PERMISSION;
    }
    if (context.checkCallingOrSelfUriPermission(uri, Intent.FLAG_GRANT_READ_URI_PERMISSION
            | Intent.FLAG_GRANT_WRITE_URI_PERMISSION)
            == PackageManager.PERMISSION_GRANTED) {
        modeFlags |= Intent.FLAG_GRANT_PERSISTABLE_URI_PERMISSION;
    }
    return modeFlags;
}
 
Example 4
Project: simple-share-android   File: DocumentsProvider.java   View Source Code Vote up 8 votes
private static int getCallingOrSelfUriPermissionModeFlags(Context context, Uri uri) {
    // TODO: move this to a direct AMS call
    int modeFlags = 0;
    if (context.checkCallingOrSelfUriPermission(uri, Intent.FLAG_GRANT_READ_URI_PERMISSION)
            == PackageManager.PERMISSION_GRANTED) {
        modeFlags |= Intent.FLAG_GRANT_READ_URI_PERMISSION;
    }
    if (context.checkCallingOrSelfUriPermission(uri, Intent.FLAG_GRANT_WRITE_URI_PERMISSION)
            == PackageManager.PERMISSION_GRANTED) {
        modeFlags |= Intent.FLAG_GRANT_WRITE_URI_PERMISSION;
    }
    if (context.checkCallingOrSelfUriPermission(uri, Intent.FLAG_GRANT_READ_URI_PERMISSION
            | Intent.FLAG_GRANT_PERSISTABLE_URI_PERMISSION)
            == PackageManager.PERMISSION_GRANTED) {
        modeFlags |= Intent.FLAG_GRANT_PERSISTABLE_URI_PERMISSION;
    }
    return modeFlags;
}
 
Example 5
Project: AppFirCloud   File: FileProvider7.java   View Source Code Vote up 8 votes
/**
 * 授权
 *
 * @param context
 * @param intent
 * @param uri
 * @param writeAble
 */
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 6
Project: AgentWeb   File: AgentWebUtils.java   View Source Code Vote up 7 votes
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 7
Project: homebank_android   File: HomeActivity.java   View Source Code Vote up 7 votes
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
    if (requestCode == LOCAL_CHOOSE_FILE_REQUEST && resultCode == RESULT_OK) {
        int flags = data.getFlags() & Intent.FLAG_GRANT_READ_URI_PERMISSION & Intent.FLAG_GRANT_PERSISTABLE_URI_PERMISSION & Intent.FLAG_GRANT_WRITE_URI_PERMISSION;
        fileUri = data.getData();
        //noinspection WrongConstant
        getContentResolver().takePersistableUriPermission(fileUri, flags);
        onFileSelected();
    }
    if (requestCode == DBX_CHOOSE_FILE_REQUEST && resultCode == RESULT_OK) {
        DbxChooser.Result result = new DbxChooser.Result(data);
        fileUri = result.getLink();

        onFileSelected();
    }
    super.onActivityResult(requestCode, resultCode, data);
}
 
Example 8
Project: keemob   File: FileChooser.java   View Source Code Vote up 7 votes
@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 9
Project: androidgithub   File: FileProvider7.java   View Source Code Vote up 7 votes
/**
 * 授权
 *
 * @param context
 * @param intent
 * @param uri
 * @param writeAble
 */
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 10
Project: FitAndroid7   File: FileProvider7.java   View Source Code Vote up 7 votes
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 11
Project: AndroidHttpUtils   File: FileProviderUtils.java   View Source Code Vote up 7 votes
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 12
Project: orgzly-android   File: ReposActivity.java   View Source Code Vote up 6 votes
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    if (BuildConfig.LOG_DEBUG) LogUtils.d(TAG, requestCode, resultCode, data);

    switch (requestCode) {
        case ACTION_OPEN_DOCUMENT_TREE_REQUEST_CODE:
            if (resultCode == RESULT_OK) {
                Uri treeUri = data.getData();

                // Persist permissions
                if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
                    grantUriPermission(getPackageName(), treeUri, Intent.FLAG_GRANT_READ_URI_PERMISSION);

                    final int takeFlags = Intent.FLAG_GRANT_READ_URI_PERMISSION |
                                          Intent.FLAG_GRANT_WRITE_URI_PERMISSION;

                    getContentResolver().takePersistableUriPermission(treeUri, takeFlags);
                }

                DirectoryRepoFragment fragment =
                        (DirectoryRepoFragment) getSupportFragmentManager()
                                .findFragmentByTag(DirectoryRepoFragment.FRAGMENT_TAG);

                fragment.updateUri(treeUri);
            }

            break;
    }
}
 
Example 13
Project: Android-BitherCompress-master   File: MainActivity.java   View Source Code Vote up 6 votes
@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 14
Project: photo-editor-android   File: MediaActivity.java   View Source Code Vote up 5 votes
@RequiresApi(api = Build.VERSION_CODES.KITKAT)
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    switch (resultCode) {
        case RESULT_CANCELED:
            break;
        case RESULT_OK:
            if (requestCode == GALLERY_INTENT_CALLED || requestCode == CAMERA_CODE
                    || requestCode == GALLERY_KITKAT_INTENT_CALLED) {
                if (requestCode == GALLERY_INTENT_CALLED) {
                    selectedImageUri = data.getData();
                    selectedImagePath = getPath(selectedImageUri);
                } else if (requestCode == GALLERY_KITKAT_INTENT_CALLED) {
                    selectedImageUri = data.getData();
                    final int takeFlags = data.getFlags()
                            & (Intent.FLAG_GRANT_READ_URI_PERMISSION | Intent.FLAG_GRANT_WRITE_URI_PERMISSION);
                    // Check for the freshest data.
                    if (selectedImageUri != null) {
                        getContentResolver().takePersistableUriPermission(
                                selectedImageUri, takeFlags);
                        selectedImagePath = getPath(selectedImageUri);
                    }
                } else {
                    selectedImagePath = selectedOutputPath;
                }

                if (UtilFunctions.stringIsNotEmpty(selectedImagePath)) {
                    // decode image size
                    BitmapFactory.Options o = new BitmapFactory.Options();
                    o.inJustDecodeBounds = true;
                    BitmapFactory.decodeFile(selectedImagePath, o);
                    // Find the correct scale value. It should be the power of
                    // 2.
                    int width_tmp = o.outWidth, height_tmp = o.outHeight;
                    Log.d("MediaActivity", "MediaActivity : image size : "
                            + width_tmp + " ; " + height_tmp);
                    final int MAX_SIZE = getResources().getDimensionPixelSize(
                            R.dimen.image_loader_post_width);
                    int scale = 1;
                    // while (true) {
                    // if (width_tmp / 2 < MAX_SIZE
                    // || height_tmp / 2 < MAX_SIZE)
                    // break;
                    // width_tmp /= 2;
                    // height_tmp /= 2;
                    // scale *= 2;
                    // }
                    if (height_tmp > MAX_SIZE || width_tmp > MAX_SIZE) {
                        if (width_tmp > height_tmp) {
                            scale = Math.round((float) height_tmp
                                    / (float) MAX_SIZE);
                        } else {
                            scale = Math.round((float) width_tmp
                                    / (float) MAX_SIZE);
                        }
                    }
                    Log.d("MediaActivity", "MediaActivity : scaling image by factor : " + scale);
                    BitmapFactory.Options options = new BitmapFactory.Options();
                    options.inSampleSize = scale;
                    bitmap = BitmapFactory.decodeFile(selectedImagePath, options);
                    _taken = true;
                    onPhotoTaken();
                    System.gc();
                }
            }
            break;
    }
    super.onActivityResult(requestCode, resultCode, data);
}