8.0读写权限

2019-04-12  本文已影响0人  小子考驾照

基于调用系统相册照相机实例
Android7.0及以上拍照获取照片无法使用file://,使用content://URI

相册

Intent photoPickerIntent = new Intent(Intent.ACTION_GET_CONTENT);
        photoPickerIntent.setType("image/*");
        activity.startActivityForResult(photoPickerIntent, requestCode);

拿到图片回调

if (null != data) {
                    cropImageUri = Uri.fromFile(fileCropUri);
                    Uri newUri = Uri.parse(PhotoUtils.getPath(this, data.getData()));
                    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
                        newUri = FileProvider.getUriForFile(this, "cn.com.kanjian.fileprovider", new File(newUri.getPath()));
                    }else if ((android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.KITKAT)){}
                    PhotoUtils.cropImageUri(this, newUri, cropImageUri, 1, 1, 200, 200, CODE_RESULT_REQUEST);
//                    Uri selectedImage = data.getData();
//                    startPhotoZoom(selectedImage);
                    return;
                }

裁剪

/**
     * @param activity
     *         当前activity
     * @param orgUri
     *         剪裁原图的Uri
     * @param desUri
     *         剪裁后的图片的Uri
     * @param aspectX
     *         X方向的比例
     * @param aspectY
     *         Y方向的比例
     * @param width
     *         剪裁图片的宽度
     * @param height
     *         剪裁图片高度
     * @param requestCode
     *         剪裁图片的请求码
     */
    public static void cropImageUri(Activity activity, Uri orgUri,
                                    Uri desUri, int aspectX, int aspectY,
                                    int width, int height, int requestCode) {
        Intent intent = new Intent("com.android.camera.action.CROP");
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {//7.0以后要加权限
            intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
        }
        intent.setDataAndType(orgUri, "image/*");
        intent.putExtra("crop", "true");
      intent.putExtra("aspectX", aspectX);
        intent.putExtra("aspectY", aspectY);
        intent.putExtra("outputX", width);
        intent.putExtra("outputY", height);
        intent.putExtra("scale", true);
        //将剪切的图片保存到目标Uri中
        intent.putExtra(MediaStore.EXTRA_OUTPUT, desUri);
        intent.putExtra("return-data", false);
        intent.putExtra("outputFormat", Bitmap.CompressFormat.JPEG.toString());
        intent.putExtra("noFaceDetection", true);
        activity.startActivityForResult(intent, requestCode);
    }

                if (extras != null) {
//                    Bitmap photo = extras.getParcelable("data");
                    // 设置头像
                    // BitmapDrawable bitmap = PictureUtils.getScaledDrawable(
                    // PersonInfoActivity.this, PATH_USEING);
                    // head_img.setImageBitmap(photo);
                    // 提交照片
                    Bitmap photo = PhotoUtils.getThumbnail(cropImageUri, ConvertUtils.dp2px(67));
//                    new DownLoadImageThread(cropImageUri,)
                            commitPhoto(photo);

Android:8.0之后读写权限授予后仍提示 Permission denied!

上一篇下一篇

猜你喜欢

热点阅读