Android 裁剪图像不会';t工作(安卓)

Android 裁剪图像不会';t工作(安卓),android,crop,android-image,Android,Crop,Android Image,对不起我的英语。我试图截取一个图像。我使用的是你可以在上找到的库和标准的Android方法。但对于某些设备,CropImage.class无法打开,我不知道为什么会发生这种情况 获取图像函数的代码 //method for get image from camera public void getPhotoCamera() { dialogBuilder.cancel(); final int REQUEST_CAMERA = 2;

对不起我的英语。我试图截取一个图像。我使用的是你可以在上找到的库和标准的Android方法。但对于某些设备,
CropImage.class
无法打开,我不知道为什么会发生这种情况

获取图像函数的代码

    //method for get image from camera
    public void getPhotoCamera() {
            dialogBuilder.cancel();
            final int REQUEST_CAMERA = 2;

            final String dir = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES) + "/test/";
            newfile = new File(dir);
            outputFileUri = Uri.fromFile(newfile);

            Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
            intent.putExtra(MediaStore.EXTRA_OUTPUT, outputFileUri);

            startActivityForResult(intent, REQUEST_CAMERA);
        }


    @Override
        protected void onActivityResult(int requestCode, int resultCode, Intent imageReturnedIntent) {
            super.onActivityResult(requestCode, resultCode, imageReturnedIntent);
            switch(requestCode) {


                case 2:
                    try{

                        Bitmap myBitmap = decodeUri(outputFileUri);
                        Bitmap orientation = ExifUtil.rotateBitmap(newfile.getAbsolutePath(), myBitmap);

                        //start crop image
                        startCrop(orientation);

                    }catch(Exception e) {
                        Log.e("Photo", e.toString());
                    }

                    break;
            }
        }

//method start crop
    public void startCrop(Bitmap bm) {
        ByteArrayOutputStream stream = new ByteArrayOutputStream();
        bm.compress(Bitmap.CompressFormat.PNG, 100, stream);
        byte[] byteArray = stream.toByteArray();

        Intent in1 = new Intent(this, Crop.class);
        in1.putExtra("image",byteArray);
        startActivity(in1);
    }
crop

@Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.p_crop);

        if(getSupportActionBar() != null) getSupportActionBar().hide();

        Intent intent = getIntent();
        Bundle extra = intent.getExtras();
        if(extra != null) {

            try{
                byte[] byteArray = getIntent().getByteArrayExtra("image");
                bmp = BitmapFactory.decodeByteArray(byteArray, 0, byteArray.length);
            } catch (Exception e) {
                Log.e("error", e.toString());}
        }


        final CropImageView cropImageView = (CropImageView)findViewById(R.id.cropImageView);
        cropImageView.setMinFrameSizeInDp(100);
        cropImageView.setImageBitmap(bmp);

        RelativeLayout cropButton = (RelativeLayout)findViewById(R.id.crop_button);
        cropButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                ByteArrayOutputStream stream = new ByteArrayOutputStream();
                cropImageView.getCroppedBitmap().compress(Bitmap.CompressFormat.PNG, 100, stream);
                byte[] byteArray = stream.toByteArray();


            }
        });
    }

为什么要将byteArray传递给
Crop.class
?改用URI

请尝试以下代码:

//method for get image from camera
public void getPhotoCamera() {
        dialogBuilder.cancel();
        final int REQUEST_CAMERA = 2;

        final String dir = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES) + "/test/";
        newfile = new File(dir);
        outputFileUri = Uri.fromFile(newfile);

        Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
        intent.putExtra(MediaStore.EXTRA_OUTPUT, outputFileUri);

        startActivityForResult(intent, REQUEST_CAMERA);
    }


@Override
    protected void onActivityResult(int requestCode, int resultCode, Intent imageReturnedIntent) {
        super.onActivityResult(requestCode, resultCode, imageReturnedIntent);
        switch(requestCode) {


            case 2:
                startCrop();
                break;
        }
    }

//method start crop
public void startCrop() {
    Intent in1 = new Intent(this, Crop.class);
    in1.putExtra("image", outputFileUri);
    startActivity(in1);
}
班级:

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.p_crop);

    if(getSupportActionBar() != null) getSupportActionBar().hide();

    Bitmap bitmap = null;
    try {
        Intent intent = getIntent();
        imageUri = intent.getParcelableExtra("image");
        bitmap = MediaStore.Images.Media.getBitmap(this.getContentResolver(), imageUri);
    } catch (Exception e) {
        e.printStackTrace();
    }

    if (bitmap != null) {
final CropImageView cropImageView = (CropImageView)findViewById(R.id.cropImageView);
    cropImageView.setMinFrameSizeInDp(100);
    cropImageView.setImageBitmap(bitmap);
    }
}