Android调用系统相机、相册、裁剪

文章地址:https://www.jianshu.com/p/3a278ffb6ef5
github源码地址:https://github.com/mahuanh/algorithmpro(该项目首页洗牌算法中用到了调用系统相机、相册、裁剪的功能)

权限

1、清单文件声明权限




2、Android6.0 动态申请权限

第三方权限请求框架: XXPermissions
https://github.com/getActivity/XXPermissions

3、Android7.0 文件读写权限适配
项目Application文件 的oncreate方法中 初始化 FileProvider
 
    private void initFileProvider() {
        // 解决7.0以上版本的FileProvider问题
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
            StrictMode.VmPolicy.Builder builder = new StrictMode.VmPolicy.Builder();
            StrictMode.setVmPolicy(builder.build());
        }
    }

调用系统相机

   public static File mCameraTmpFile;
    /**
     * 打开相机
     */
    public void openCamera() {
        Intent cameraIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
        if (cameraIntent.resolveActivity(getActivity().getPackageManager()) != null) {
            mCameraTmpFile = createTmpFile(this);  // 设置系统相机拍照后的输出路径, 创建临时文件
            cameraIntent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(mCameraTmpFile));
            MyLog.info("==openCamera,mCameraTmpFile-->" + mCameraTmpFile);
            startActivityForResult(cameraIntent, 1001);
        } else {
            MyToast.showToast("没有系统相机");
        }
    }

调用系统相册

    /**
     * 打开相册
     */
    public void openZooM() {
        Intent intent = new Intent();
        intent.addCategory(Intent.CATEGORY_OPENABLE);
        intent.setType("image/*");
        if (Build.VERSION.SDK_INT < 19) {
            intent.setAction(Intent.ACTION_GET_CONTENT);
        } else {
            intent.setAction(Intent.ACTION_OPEN_DOCUMENT);
        }
        getActivity().startActivityForResult(intent, 1002);
    }
android 拍照或者相册选择照片后 调用裁剪,提示“无法加载此图片”

解决方法是在裁剪的代码里加上下面的代码:

 if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
            intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
        }

调用系统裁剪

    //裁剪
    private void startPhotoZoom(Uri uri) {
        Intent intent = new Intent("com.android.camera.action.CROP");
        intent.setDataAndType(uri, "image/*");
        intent.putExtra("crop", "true");
        intent.putExtra("scale", true);// 去黑边
        intent.putExtra("scaleUpIfNeeded", true);// 去黑边
        intent.putExtra("aspectX", 1);
        intent.putExtra("aspectY", 1);
        intent.putExtra("outputX", 400);
        intent.putExtra("outputY", 400);
       //解决 android 拍照或者相册选择照片后 调用裁剪,提示“无法加载此图片”  问题
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
            intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
        }
        intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(createTmpFile(this)));
        intent.putExtra("outputFormat", Bitmap.CompressFormat.JPEG.toString());
        intent.putExtra("return-data", false);
        intent.putExtra("noFaceDetection", true);

        startActivityForResult(intent, 1003);
    }
    private File mCropOutPutFile;

    public File createTmpFile(Context context) {
        String state = Environment.getExternalStorageState();
        if (state.equals(Environment.MEDIA_MOUNTED)) { // 已挂载
//          File pic = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES);
            File pic = Environment.getExternalStorageDirectory();
            if (!pic.exists()) {
                pic.mkdirs();
            }
            MyLog.e("Lee", "已挂载: pic dir = " + pic.getAbsolutePath());
            String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss", Locale.CHINA).format(new Date());
            String fileName = TextUtils.concat("multi_image_", timeStamp, ".jpg").toString();
            mCropOutPutFile = new File(pic, fileName);
            return mCropOutPutFile;
        } else {
            File cacheDir = context.getCacheDir();
            MyLog.e("Lee", "未挂载: pic dir = " + cacheDir.getAbsolutePath());
            String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss", Locale.CHINA).format(new Date());
            String fileName = TextUtils.concat("multi_image_", timeStamp, ".jpg").toString();
            mCropOutPutFile = new File(cacheDir, fileName);
            return mCropOutPutFile;
        }
    }
    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        String absoluteImagePath;
        if (resultCode != Activity.RESULT_OK) {
            return;
        }
        switch (requestCode) {
            case 1001://相机
                if (mCameraTmpFile != null && mCameraTmpFile.exists()) {
                    //开始裁剪
                    startPhotoZoom(Uri.fromFile(mCameraTmpFile));
                }
                break;
            case 1002://相册
                Uri uri = data.getData();
                MyLog.info(uri + "");
                //开始裁剪
                startPhotoZoom(uri);

                break;
            case 1003://裁剪
                if (mCropOutPutFile != null) {
                    if (data == null) {
                        return;
                    }
                    absoluteImagePath = mCropOutPutFile.getAbsolutePath();
                 ......
                }
                break;
        }
    }

你可能感兴趣的:(Android调用系统相机、相册、裁剪)