1, get the address of the saved file
private static final String image_file_location = environment.getexternalstoragedirectory () + "/temp.jpg";p rivate Uri Imageuri;imageuri = Uri.fromfile (new File (image_file_location));
2. Click events for photo albums and photo buttons
Brochure Imageviewlocalphoto.setonclicklistener (new Onclicklistener () {public void OnClick (View v) {Intent Intent = new Inten T (intent.action_get_content, NULL); Intent.setdataandtype (mediastore.images.media.external_content_uri,//content ://media/external/images/mediaimage_unspecified); Startactivityforresult (Intent, photozoom);}); /Photo Imageviewtakephoto.setonclicklistener (New Onclicklistener () {public void OnClick (View v) {Intent Intent = new Intent ( Mediastore.action_image_capture); Intent.putextra (Mediastore.extra_output, Uri.fromfile (new File) ( Environment.getexternalstoragedirectory (), "temp.jpg")); System.out.println ("=============" + environment.getexternalstoragedirectory ()); Startactivityforresult (Intent, photohraph);});
3. In the Onactivityresult callback method
Photo is different from the URI that the local picture passed in
Photo Display Picture Page if (Requestcode = = photohraph) {startphotozoom (Imageuri);} Read the album zoom picture Display Picture Page if (Requestcode = = photozoom) {startphotozoom (Data.getdata ());}
4, method
public void Startphotozoom (URI uri, int flag) {Intent Intent = new Intent ("Com.android.camera.action.CROP"); Intent.setdataandtype (URI, "image/*"), Intent.putextra ("crop", "true"), Intent.putextra (Mediastore.extra_output, Imageuri);//Image Output Intent.putextra ("OutputFormat", Bitmap.CompressFormat.JPEG.toString ()); Intent.putextra (" Nofacedetection ", true); Intent.putextra (" Return-data ", false);//callback Method Data.getextras (). getparcelable (" Data ") The returned data is empty startactivityforresult (intent, photoresoult);}
5, the callback method returns the result
@Overrideprotected void Onactivityresult (int requestcode, int resultcode, Intent data) { //processing result processing after scaled picture if (re Questcode = = Photoresoult) { flag_layout = "Layout2"; Bundle extras = Data.getextras (); if (extras! = null) { photo = extras.getparcelable ("Data");} if (photo==null)//Load local { Bitmap BM = bitmapfactory.decodefile (environment.getexternalstoragedirectory () + "/ Temp.jpg "); Imageview.setimagebitmap (BM); } else//loading the received picture data { imageview.setimagebitmap (photo);} } Super.onactivityresult (Requestcode, ResultCode, data);}
The traditional method is: 1, does not add this sentence Intent.putextra (mediastore.extra_output, Imageuri);//Image output,
2, this sentence instead returns the data as True:intent.putExtra ("Return-data", true);
3. There is no post-cut output image, and the returned image data is received by Data.getextras (). getparcelable ("Data").
Description: After the test, the traditional method often crashes, reported too large errors in the binding data, so instead of using this example of the method is more useful, is to cut the picture, directly loaded into the local image is good
BUG: Suppose you delete a temp.jpg file. The temp.jpg file will be truncated when the photo is taken. The local picture is fine, and it's no problem changing back to the camera.
Photos and local pictures in Android projects