Android Display album pictures and camera Photos

Source: Internet
Author: User

First look at the most important mainactive class:

1  Public classMainactivityextendsappcompatactivity {2 3     Private Final intFrom_album = 1;//get photos from albums4     Private Final intFrom_camera = 2;//means getting photos from the camera5     PrivateImageView ImageView;6 7 @Override8     protected voidonCreate (Bundle savedinstancestate) {9         Super. OnCreate (savedinstancestate);Ten Setcontentview (r.layout.activity_main); One     } A  -     //Open Album -      Public voidonclickalbum (view view) { theIntent Intent =NewIntent (Intent.action_pick, MediaStore.Images.Media.EXTERNAL_CONTENT_URI); - Startactivityforresult (Intent, from_album); -     } -  +     //turn on the camera -      Public voidOnclickcamera (view view) { +Intent Intent =NewIntent (mediastore.action_image_capture); A Startactivityforresult (Intent, from_camera); at     } -  - @Override -     protected voidOnactivityresult (intRequestcode,intResultCode, Intent data) { -  -         //return from album in         if(Requestcode = = From_album && ResultCode = = ACTIVITY.RESULT_OK && Data! =NULL){ -ImageView =(ImageView) Findviewbyid (R.id.imageview); toUri Imageuri =Data.getdata (); +Contentresolver CR = This. Getcontentresolver (); -             Try { theBitmap Bitmap =Bitmapfactory.decodestream (Cr.openinputstream (Imageuri)); + Imageview.setimagebitmap (bitmap); A}Catch(FileNotFoundException e) { theLOG.E ("Exception", E.getmessage (), e); +             } -         } $  $         //return from Camera -         if(Requestcode = = From_camera && ResultCode = = ACTIVITY.RESULT_OK && Data! =NULL){ -ImageView =(ImageView) Findviewbyid (R.id.imageview); theBitmap photo = (Bitmap) Data.getextras (). Get ("Data")); - Wuyi imageview.setimagebitmap (photo); the         } -  Wu         Super. Onactivityresult (Requestcode, ResultCode, data); -     } About  $}

The name of the handler function for the above two buttons is defined in the layout, and the layout is as follows: Two button (one opens the album, one opens the camera), and the other one ImageView

1 <?XML version= "1.0" encoding= "Utf-8"?>2 <Android.support.constraint.ConstraintLayoutxmlns:android= "Http://schemas.android.com/apk/res/android"3 Xmlns:app= "Http://schemas.android.com/apk/res-auto"4 Xmlns:tools= "Http://schemas.android.com/tools"5 Android:layout_width= "Match_parent"6 Android:layout_height= "Match_parent"7 Tools:context= "Com.meitu.graydemo.MainActivity">8 9 <LinearLayoutTen     Android:layout_width= "368DP" One Android:layout_height= "Wrap_content" A android:orientation= "vertical" - Tools:layout_editor_absolutey= "0DP" - Tools:layout_editor_absolutex= "8DP"> the  -     <LinearLayout -         Android:id= "@+id/buttonlayout" - Android:layout_width= "Fill_parent" + Android:layout_height= "Wrap_content" - android:orientation= "Horizontal"> +  A         <Button at             Android:id= "@+id/button" - Android:layout_width= "Wrap_content" - Android:layout_height= "Wrap_content" - Android:onclick= "Onclickalbum" - Android:text= "Open album" - Tools:layout_editor_absolutex= "16DP" in Tools:layout_editor_absolutey= "16DP" /> -  to         <Button +             Android:id= "@+id/button2" - Android:layout_width= "Wrap_content" the Android:layout_height= "Wrap_content" * Android:onclick= "Onclickcamera" $ Android:text= "Open Camera"Panax Notoginseng Tools:layout_editor_absolutex= "280DP" - Tools:layout_editor_absolutey= "16DP" /> the     </LinearLayout> +  A     <ImageView the         Android:id= "@+id/imageview" + Android:layout_width= "Wrap_content" - Android:layout_height= "Wrap_content" $ App:srccompat= "@android: Color/holo_blue_bright" $ Tools:layout_editor_absolutex= "16DP" - Tools:layout_editor_absolutey= "48DP" /> -  the  - </LinearLayout>Wuyi  the  -  Wu  - </Android.support.constraint.ConstraintLayout>

Android Display album pictures and camera 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.