Solve the Problem of cropping com. android. camera. action. CROP and intent. putExtra ("return-data", true );,
Recently, I was uploading an image. before uploading the image, I needed to crop the image. When I encountered a pitfall, I ran normally on other mobile phones, but I encountered a Problem On Xiaomi mobile phones, the selected image cannot be cropped and is directly flashed back. It has been fixed now! The previous issues may be highlighted in red.
// Select an image
Private void choosePhotos (){
Intent intent = new Intent (Intent. ACTION_PICK, android. provider. MediaStore. Images. Media. EXTERNAL_CONTENT_URI );
Intent. setDataAndType (MediaStore. Images. Media. EXTERNAL_CONTENT_URI, "image /*");
StartActivityForResult (intent, CHOOSE_PICTURE );
}
@ Override
Public void onActivityResult (int requestCode, int resultCode, Intent data ){
Super. onActivityResult (requestCode, resultCode, data );
Switch (requestCode ){
Case CHOOSE_PICTURE:
// Crop the image
CutImage (data. getData ());
Break;
Case CROP_SMALL_PICTURE:
If (data! = Null ){
Try {
Bitmap head = BitmapFactory. decodeStream (getContentResolver (). openInputStream (uritempFile ));
// Display the cropped image on the page
SetImageView (head );
} Catch (FileNotFoundException e ){
E. printStackTrace ();
}
}
Break;
}
}
// Crop the image
Protected void cutImage (Uri uri ){
// Com. android. camera. action. CROP. This action is used to CROP images.
Intent intent = new Intent ("com. android. camera. action. CROP ");
Intent. setDataAndType (uri, "image /*");
// Set Cropping
Intent. putExtra ("crop", "true ");
// AspectX aspectY is the aspect ratio
Intent. putExtra ("aspectX", 1 );
Intent. putExtra ("aspectY", 1 );
// OutputX outputY indicates the width and height of the cropped image.
Intent. putExtra ("outputX", 150 );
Intent. putExtra ("outputY", 150 );
// Return-data is not required here (explained later)
// Intent. putExtra ("return-data", true );
UritempFile = Uri. parse ("file: //" + "/" + Environment. getExternalStorageDirectory (). getPath () + "/" + "temp_image.jpg ");
Intent. putExtra (MediaStore. EXTRA_OUTPUT, uritempFile );
Intent. putExtra ("outputFormat", Bitmap. CompressFormat. JPEG. toString ());
StartActivityForResult (intent, CROP_SMALL_PICTURE );
}
// Image echo
Private void setImageView (Bitmap bitmap ){
Bitmap bitmap_bg = Bitmap. createBitmap (bitmap. getWidth (), bitmap. getHeight (), Bitmap. Config. ARGB_8888 );
Canvas canvas = new Canvas (bitmap_bg );
Paint paint = new Paint ();
Paint. setAntiAlias (true );
Paint. setColor (Color. GREEN );
// The plotting Mode is used here. PorterDuff. Mode. SRC_IN indicates that the two layers display the content of the above layers.
Canvas. drawOval (new RectF (0, 0, bitmap. getWidth (), bitmap. getHeight (), paint );
Paint. setXfermode (new porterduxfermode (PorterDuff. Mode. SRC_IN ));
Canvas. drawBitmap (bitmap, 0, 0, paint );
Drawable drawable = new BitmapDrawable (bitmap_bg );
// PersonImage is the id to be assigned a value.
Image. setImageDrawable (drawable );
}
OK! Select crop from the photo library and upload it!
Intent. putExtra ("return-data", true );
If it is a large image, the image can be directly converted into a uri for transmission.