Android Development to get photo albums and get photo photos

Source: Internet
Author: User

During the development of Android, we may read the photos in the phone or take pictures from the camera, this is the two common ways to get pictures, in the process of doing the project will often encounter, the following to introduce the two ways to obtain:

1. Get photos from your local albums:

This is usually the way to write it.

protected void Getimagefromalbum () {      new  Intent (intent.action_pick);      Intent.settype ("image/*"); // Photo type       Startactivityforresult (Intent, request_code_pick_image);  }  

2. Get photos from the camera

This is usually the way to write it.

protected void Getimagefromcamera () {     = environment.getexternalstoragestate ();      if (State.equals (environment.media_mounted)) {         new Intent ("Android.media.action.IMAGE_CAPTURE ");            Startactivityforresult (Getimagebycamera, Request_code_capture_cameia);      Else {         "Please confirm that you have inserted the SD card", Toast.length_long). Show ();      }  }  

After writing this method, generally we still need to respond to this method to get the picture

@Overrideprotected voidOnactivityresult (intRequestcode,intResultCode, Intent data) {      if(Requestcode = =request_code_pick_image) {URI Uri=Data.getdata (); //To do find the path of pic                } Else if(Requestcode = =Request_code_capture_cameia) {URI Uri=Data.getdata (); //To do find the path of pic}}

However, sometimes we find that the URI that we get when we take a photo with the camera is null, because Android encapsulates the captured image in bundles and passes it back, but depending on how the machine gets the photos, the camera may be able to pass
Inten.getdata () gets the URI
And then get the path of the data according to the URI, in the encapsulation into bitmap, but sometimes some cameras get the null, what should we do?
In fact, we should get the data from bundles
(Bitmap) Bundle.get ("Data")
Gets the bitmap data to the camera picture.
In order to be able to adapt to both of these situations, we should make a judgment when we get the picture. We can make a judgment in response:

@Overrideprotected voidOnactivityresult (intRequestcode,intResultCode, Intent data) {         if(Requestcode = =request_code_pick_image) {URI Uri=Data.getdata (); //To do find the path of a pic by Uri       } Else if(Requestcode = =Request_code_capture_cameia) {URI Uri=Data.getdata (); if(uri = =NULL) {//Use bundle to get DataBundle bundle =Data.getextras (); if(Bundle! =NULL) {Bitmap photo= (Bitmap) bundle.get ("Data");//Get bitmap               } Else{Toast.maketext (Getapplicationcontext (),"err****", Toast.length_long). Show (); return; }}Else{                  //To do find the path of a pic by Uri }}}

The subsequent process needs to be transformed into the corresponding picture file by bitmap. However, the final picture is compressed . If we want the original photo taken by the camera

Intent Getimagebycamera = new Intent ("Android.media.action.IMAGE_CAPTURE");
Then we directly tell the file to save to the specified path filepath, and then directly in the
Onactivityresult (int requestcode, int resultcode, Intent data)
Pass the FilePath in the past

Sample code

protected voidGetimagefromcamera () {String State=environment.getexternalstoragestate (); if(State.equals (environment.media_mounted)) {Intent Getimagebycamera=NewIntent ("Android.media.action.IMAGE_CAPTURE"); String Out_file_path=Constant.saved_image_dir_path; File dir=NewFile (Out_file_path); if(!dir.exists ())              {Dir.mkdirs (); } Capturepath= Constant.saved_image_dir_path + system.currenttimemillis () + ". jpg"; Getimagebycamera.putextra (Mediastore.extra_output, Uri.fromfile (NewFile (Capturepath)); Getimagebycamera.putextra (mediastore.extra_video_quality,1);          Startactivityforresult (Getimagebycamera, Constant.request_code_capture_cameia); }          Else{Toast.maketext (Getapplicationcontext (),"Please make sure the SD card is plugged in", Toast.length_long). Show (); }      }  

Android Development to get photo albums and get photo photos

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.