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
Album 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 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, through Data.getextras (). getparcelable ("Data") to receive the returned image data
Description: After the test, the traditional method often crashes, reported too large error binding data, so instead of using this example of the method is more useful, is to cut the picture, the direct loading of local pictures just fine
BUG: If you delete the temp.jpg file, the photo will be cut off the temp.jpg file, the local picture is no problem, and then change back to take pictures also no problem
Photos and local pictures in Android projects