从图像选择器裁剪的图像

时间:2019-03-08 07:30:53

标签: android

我正在尝试从图像选择器中获取裁剪的图像。我可以选择一个图像,然后看到裁剪后的矩形。但是在裁剪恢复到我的活动之后,我的uri始终为null ...将不会调用“ onActivityResult”。

我的代码:

        iv.setOnClickListener(new View.OnClickListener(){
            @Override
            public void onClick(View view) {
                Intent pickImageIntent = new Intent(Intent.ACTION_PICK, MediaStore.Images.Media.EXTERNAL_CONTENT_URI);

                int imageX = iv.getMeasuredWidth();
                int imageY = iv.getMeasuredHeight();

                pickImageIntent.setType("image/*");
                pickImageIntent.putExtra("crop", "true");
                pickImageIntent.putExtra("outputX", imageX);
                pickImageIntent.putExtra("outputY", imageY);
                pickImageIntent.putExtra("aspectX", imageX);
                pickImageIntent.putExtra("aspectY", imageY);
                pickImageIntent.putExtra("scale", true);
                pickImageIntent.putExtra(MediaStore.EXTRA_OUTPUT, uri);
                startActivityForResult(pickImageIntent,REQ_CODE_PICK_GALLERY);
            }
        });

也在呼叫

pickImageIntent.putExtra("return-data", true);

@Override
    protected void onResume(){
        super.onResume();
        iv.setImageURI(uri);
    }

也不起作用...

这些是我的权限:

<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.CAMERA" />

我也尝试过:

pickImageIntent.putExtra("outputFormat", Bitmap.CompressFormat.PNG.toString());
pickImageIntent.putExtra("return-data", true);

并获得onActivityResult中的位图有效,但是imagesize太小。感觉就像

pickImageIntent.putExtra("outputX", imageX);
pickImageIntent.putExtra("outputY", imageY);

被忽略了,我该怎么办?

1 个答案:

答案 0 :(得分:0)

您必须覆盖“ onActivityResult”,而不是“ onResume”。 这是一个可以帮助您的示例

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

    switch(requestCode) { 
    case REQ_CODE_PICK_IMAGE:
        if(resultCode == RESULT_OK){  
            Uri selectedImage = imageReturnedIntent.getData();
            String[] filePathColumn = {MediaStore.Images.Media.DATA};

            Cursor cursor = getContentResolver().query(selectedImage, filePathColumn, null, null, null);
            cursor.moveToFirst();

            int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
            String filePath = cursor.getString(columnIndex);
            cursor.close();


            Bitmap yourSelectedImage = BitmapFactory.decodeFile(filePath);
        }
    }
}