Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
236 views
in Technique[技术] by (71.8m points)

android - capture image from camera and crop not working

here is my camera intent.

            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);

in onActivityresult

if (requestCode == PublicValues.IMAGE_FROM_CAMERA) { 

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

and crop intent

            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);

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

crash report is like this:-

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)
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

use Gradle :

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

and try this:

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...