简体   繁体   English

从图像选择器裁剪的图像

[英]Cropped image from the image picker

I am trying to get a cropped image from the image picker. 我正在尝试从图像选择器中获取裁剪的图像。 I can choose an image and see the cropped rectangle. 我可以选择一个图像,然后看到裁剪后的矩形。 But after cropping returned to my activity, my uri is always null... "onActivityResult" will not be called. 但是在裁剪返回到我的活动之后,我的uri始终为null ...将不会调用“ onActivityResult”。

My code: 我的代码:

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

also calling 也打电话

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

or 要么

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

does not work either... 也不起作用...

These are my permissions: 这些是我的权限:

<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" />

I also tried: 我也尝试过:

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

and get the bitmap in onActivityResult works, but the imagesize is too small. 并在onActivityResult获得位图,但是imagesize大小太小。 It feels like 感觉像

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

is being ignored, what can I do? 被忽略,我该怎么办?

You have to override the "onActivityResult", not the "onResume". 您必须覆盖“ onActivityResult”,而不是“ onResume”。 Here is a sample that may helps you 这是一个可以帮助您的示例

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM