从相机捕获图像并且裁剪不起作用 [英] capture image from camera and crop not working

查看:121
本文介绍了从相机捕获图像并且裁剪不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我的相机意图。

            File file = getExternalFilesDir(Environment.DIRECTORY_DCIM);
            file.mkdirs();
            File output = new File(file, "profile_image");
            if (output.exists()) {
                output.delete();
            }
            Intent captureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);

            captureIntent.putExtra(MediaStore.EXTRA_OUTPUT, output);
            startActivityForResult(captureIntent, PublicValues.IMAGE_FROM_CAMERA);

在onActivityresult

in onActivityresult

if (requestCode == PublicValues.IMAGE_FROM_CAMERA) { 

                if (mPhotoUri != null) {
                    performCrop(mPhotoUri);
                } else {
                    pictureUri = data.getData();
                    performCrop(pictureUri);
                }

和作物意图

            Intent cropIntent = new Intent("com.android.camera.action.CROP");
            cropIntent.setDataAndType(picUri, "image/*");
            cropIntent.putExtra("crop", "true"); // set crop properties
            cropIntent.putExtra("aspectX", 1);  // indicate aspect of desired crop(ratio)
            cropIntent.putExtra("aspectY", 1);
            cropIntent.putExtra("return-data", true); // retrieve data on return
            cropIntent.putExtra("outputX", profileImage.getWidth());   // indicate output X and Y
            cropIntent.putExtra("outputY", profileImage.getHeight());

            startActivityForResult(cropIntent, PublicValues.IMAGE_CROP);

它与API 19设备兼容,但在棉花糖设备中崩溃。

it works fine with API 19 device but crashes in Marshmallow device.

崩溃报告是这样的:-

Caused by android.os.FileUriExposedException: file:///storage/emulated/0/WhatsApp/Media/WhatsApp Images/IMG-20170721-WA0014.jpg exposed beyond app through Intent.getData()
android.os.StrictMode.onFileUriExposed (StrictMode.java:1813)
android.net.Uri.checkFileUriExposed (Uri.java:2360)
android.content.Intent.prepareToLeaveProcess (Intent.java:8981)
android.content.Intent.prepareToLeaveProcess (Intent.java:8942)
android.app.Instrumentation.execStartActivity (Instrumentation.java:1583)
android.app.Activity.startActivityForResult (Activity.java:4228)
android.support.v4.app.BaseFragmentActivityJB.startActivityForResult (BaseFragmentActivityJB.java:50)
android.support.v4.app.FragmentActivity.startActivityForResult (FragmentActivity.java:79)


解决方案

使用Gradle:

compile 'com.theartofdev.edmodo:android-image-cropper:2.3.+'

并尝试以下操作:

private void performCrop(Uri imageUri) {
    CropImage.activity(imageUri)
            .setGuidelines(CropImageView.Guidelines.ON)
            .setCropShape(CropImageView.CropShape.RECTANGLE)
            .setAspectRatio(1, 1)
            .setMultiTouchEnabled(true)
            .start(this);
}

这篇关于从相机捕获图像并且裁剪不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆