Intent ("Com.android.camera.action.CROP"), central data, mediastore.extra_output, and Return-data
Data:parcelable type of data type passed in
MediaStore.EXTRA_OUTPUT:Uri type custom crop output where the picture is stored
The Return-data:boolean type has return data (with return value, return bitmap, no return value because the URI is similar to file, all operations such as cropping save the data in the URI, The corresponding URI is already held and no superfluous is required to return to bitmap. )
The truncation map uses the URI, the small chart saves the data with the bitmap.
- Use bitmap and return data
- Using URIs does not return data
The source of the picture is a photo album and Photo:
Photo Album with large image URI, small figure bitmap data storage method.
Prepare the URI you want to use
private static final String image_file_location = "file:///sdcard/temp.jpg";//temp FILE Uri Imageuri = Uri.parse ( image_file_location);//the Uri to store the big bitmap
One, from the album to cut large map:
Intent Intent = new Intent (intent.action_get_content, null); Intent.settype ("image/*"); Intent.putextra ("Crop", "true"); Intent.putextra ("Aspectx", 2); Intent.putextra ("Aspecty", 1); Intent.putextra ("Outputx", "Max"),Intent.putextra ("Outputy"), Intent.putextra ("scale", true); Intent.putextra ("Return-data", false); Intent.putextra (Mediastore.extra_output, Imageuri); Intent.putextra (" OutputFormat ", Bitmap.CompressFormat.JPEG.toString ()); Intent.putextra ("Nofacedetection", true); Startactivityforresult (Intent, choose_big_picture);
Second, from the album to cut small map
Intent Intent = new Intent (intent.action_get_content, null); Intent.settype ("image/*"); Intent.putextra ("Crop", "true"); Intent.putextra ("Aspectx", 2); Intent.putextra ("Aspecty", 1); Intent.putextra ("Outputx", "Max"), Intent.putextra ("Outputy", "n"); Intent.putextra ("scale", true); Intent.putextra ("Return-data", true); Intent.putextra ("OutputFormat", Bitmap.CompressFormat.JPEG.toString ()); Intent.putextra ("Nofacedetection", true); Startactivityforresult (Intent, choose_small_picture);
Third, the corresponding Onactivityresult can handle the returned data in this way
Switch (requestcode) {case choose_big_picture:log.d (TAG, "choose_big_picture:data =" +< span> data); if (Imageuri! = null ) {Bitmap Bitmap = decodeuriasbitmap (Imageuri); Imageview.setimagebitmap (bitmap); } break , Case choose_small_picture:if (data! = null ) {Bitmap Bitmap = Data.getparcelableextra (" Data "); Imageview.setimagebitmap (bitmap); } else {log.e (TAG, "choose_small_picture:data =" + data);} Break , default : Break ;}
Private Bitmap Decodeuriasbitmap (Uri uri) { Bitmap Bitmap = null; Try { bitmap = Bitmapfactory.decodestream (getcontentresolver () . Openinputstream (URI)); } catch (FileNotFoundException e) { e.printstacktrace (); return null;} return bitmap;}
:
Large
Small map
Photo is a bit special, you know, now the Android smartphone camera is millions of pixels, the pictures are very large. Therefore, we can not use the same as the photo album Bitmap, regardless of the large map of the small map to use the URI to operate uniformly.
First, prepare the URI you want to use:
private static final String image_file_location = "file:///sdcard/temp.jpg"; Uri Imageuri = Uri.parse (image_file_location);
Second, using Mediastore.action_image_capture can easily call camera program to take pictures:
Intent Intent = new Intent (mediastore.action_image_capture); Intent.putextra (Mediastore.extra_output, Imageuri); Startactivityforresult (Intent, take_big_picture);//or take_small_picture
Thirdly, we can get the returned data (URI) in Onactivityresult, and pass the URI to the program.
Switch (requestcode) { case take_big_picture: log.d (TAG, "take_big_picture:data =" + data);//it seems t o be null cropimageuri (Imageuri,N, crop_big_picture); Break; Case Take_small_picture: log.i (TAG, "take_small_picture:data =" + data); TODO sent to Crop Cropimageuri (Imageuri, N,crop_small_picture); break; Default: Break;}
You can see, whether it is a big picture or a small picture, are used by the URI, only the size is different. We encapsulate this operation in a single method.
private void Cropimageuri (URI uri, int outputx, int outputy, int requestcode) { Intent Intent = new Intent ("com.an Droid.camera.action.CROP "); Intent.setdataandtype (URI, "image/*"); Intent.putextra ("Crop", "true"); Intent.putextra ("Aspectx", 2); Intent.putextra ("Aspecty", 1), Intent.putextra ("Outputx", OUTPUTX); Intent.putextra ("Outputy", Outputy); Intent.putextra ("scale", true); Intent.putextra (Mediastore.extra_output, URI); Intent.putextra ("Return-data", False); Intent.putextra ("OutputFormat", Bitmap.CompressFormat.JPEG.toString ()); Intent.putextra ("Nofacedetection", true); No Face Detection startactivityforresult (Intent, Requestcode);}
Four, the last step, we have passed the data into the crop picture program, the next thing to do is to process the returned data.
Switch (requestcode) {case crop_big_picture://from Crop_big_picture log.d (TAG, "crop_big_picture:data = + data); if (Imageuri! = null) { Bitmap Bitmap = decodeuriasbitmap (imageuri); Imageview.setimagebitmap (bitmap); } Break; Case crop_small_picture:if (imageuri! =null) {Bitmap Bitmap = decodeuriasbitmap (imageuri); imageview.se Timagebitmap (bitmap); } else {log.e (TAG, "crop_small_picture:data =" + data);} Break,default: Break;}
:
Android big picture cropped phone take photos and choose Note from album