实现头像的拍摄,相册选择并进行裁剪 android

/**
     * 裁剪图片方法实现
     *
     * @param uri
     */
    public void startPhotoZoom(Uri uri) {
        if (uri == null) {
            finish();
            return;
        }
        String name = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date()) + "cut.jpg";
        cropPhoto = new File(getExternalCacheDir(), name);
        //m_ResultImagePath = CropPhoto.getPath();
        mPhotoUri = Uri.fromFile(cropPhoto);
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
            cropPhoto = uriToFileApiQ(uri, this);
            mPhotoUri = FileProvider.getUriForFile(this,
                    getPackageName() + ".provider", cropPhoto);

        }
        Intent intent = new Intent("com.android.camera.action.CROP");
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
            intent.setDataAndType(mPhotoUri, "image/*");
        } else {
            intent.setDataAndType(uri, "image/*");
        }

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
            intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION | Intent.FLAG_GRANT_WRITE_URI_PERMISSION); //添加这一句表示对目标应用临时授权该Uri所代表的文件
        }
        intent.putExtra("crop", "true");
        intent.putExtra("scale", true);
        if (Build.MANUFACTURER.equals("HUAWEI")) {
            intent.putExtra("aspectX", 9998);
            intent.putExtra("aspectY", 9999);
        } else {
            intent.putExtra("aspectX", 1);
            intent.putExtra("aspectY", 1);
        }

        intent.putExtra("outputX", 200);
        intent.putExtra("outputY", 200);
        intent.putExtra("return-data", false);
        intent.putExtra(MediaStore.EXTRA_OUTPUT, mPhotoUri);
        intent.putExtra("outputFormat", Bitmap.CompressFormat.JPEG.toString());
        try {
            startActivityForResult(intent, SELECT_REQUEST_CODE);//这里的RESULT_REQUEST_CODE是在startActivityForResult里使用的返回值。
        } catch (Exception e) {
            // 解决截取后部分机型秒退
            e.printStackTrace();
        }
    }


    /**
     * android10   api30以上,进程隔离,沙盒模式,申请权限没有用,这个是文件转换
     **/
    @TargetApi(Build.VERSION_CODES.Q)
    public static File uriToFileApiQ(Uri uri, Context context) {
        File file = null;
        if (uri == null) return file;
        //android10以上转换
        if (uri.getScheme().equals(ContentResolver.SCHEME_FILE)) {
            file = new File(uri.getPath());
        } else if (uri.getScheme().equals(ContentResolver.SCHEME_CONTENT)) {
            //把文件复制到沙盒目录
            ContentResolver contentResolver = context.getContentResolver();
//            String displayName = "userHead"
//                    +"."+ "jpg";

            String displayName = "userHead" + System.currentTimeMillis() + Math.round((Math.random() + 1) * 1000)
                    + "." + MimeTypeMap.getSingleton().getExtensionFromMimeType(contentResolver.getType(uri));
            try {
                InputStream is = contentResolver.openInputStream(uri);
                File cache = new File(context.getExternalFilesDir(Environment.DIRECTORY_PICTURES).getAbsolutePath(), displayName);
                FileOutputStream fos = new FileOutputStream(cache);
                FileUtils.copy(is, fos);
                file = cache;
                fos.close();
                is.close();
                cache = null;
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

        return file;
    }

你可能感兴趣的:(android)