Screenshots of photos and local images in Android projects

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.