android4.4获取相册,剪裁后保存失败

我是参考这文章解决的:http://blog.csdn.net/zhanlanmg/article/details/43368585。

主要是4.4相册选择图片后返回的uri不同,直接上代码:

private Uri imageUri;

//打开系统相册

File file = new File(Environment.getExternalStorageDirectory(), "hello_choose.jpg");//用于存储选择的照片
        try {
            if (file.exists()){
                file.delete();
            }
            file.createNewFile();
        } catch (IOException e) {
            e.printStackTrace();
        }
        imageUri = Uri.fromFile(file);
        Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
//        intent.setType("image/*");//系统图库
        intent.setDataAndType(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, "image/*");//图片选择器
        intent.putExtra("crop", true);//允许缩放和裁剪、图片的输出位置
        intent.putExtra("scale", true);
        intent.putExtra(MediaStore.EXTRA_OUTPUT, imageUri);
        intent.setAction(Intent.ACTION_PICK);
        intent.setData(android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
        startActivityForResult(intent, TAKE_GALLERY);

//相册返回处理,调用剪裁

case TAKE_GALLERY://相册返回
                if (resultCode == RESULT_OK){
                    if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.KITKAT){
                        Intent intent = new Intent("com.android.camera.action.CROP");
                        intent.setDataAndType(data.getData(), "image/*");
                        // 可裁剪状态
                        intent.putExtra("crop", true);
                        intent.putExtra("scale", true);
                        intent.putExtra("return-data", false);
                        intent.putExtra("outputFormat", Bitmap.CompressFormat.JPEG.toString());
                        intent.putExtra("noFaceDetection", true);
                        /** 不用file,直接使用路径,不行 **/
//                        File file = new File(getBackgroundFilename());
                        File file = new File(Environment.getExternalStorageDirectory(), "hello_choose.jpg");
                        Uri uri1 = Uri.fromFile(file);
                        intent.putExtra(MediaStore.EXTRA_OUTPUT, uri1);
                        startActivityForResult(intent, TAKE_CROP);
                    }else {
                        startImageZoom(imageUri);
                    }
                }
                break;

//剪裁返回

扫描二维码关注公众号,回复: 940755 查看本文章

case TAKE_CROP://剪裁返回
    if (resultCode == RESULT_OK){
        showImageZoom(imageUri, iv);
    }
    break;

猜你喜欢

转载自blog.csdn.net/cwwei20122012/article/details/78616919