[Android] Select a local image (open the media library and select an image)

Source: Internet
Author: User

In this survey, I want to achieve the following: Click the Pictures button to obtain all the pictures in the mobile phone, select an image, and display it in the imageview.

Application Scope: select an image when uploading an image, similar to "browsing ".

Effect:

All images are listed, including directories.

In Activity
Action contains a "action_get_content" String constant, which allows users to select data of a specific type and return the URI of the data. we can use this constant and set the type to "image/*" to obtain all images in the Android mobile phone.

 

 

Main. xml:

<? XML version = "1.0" encoding = "UTF-8"?> <Br/> <linearlayout xmlns: Android = "http://schemas.android.com/apk/res/android" <br/> Android: Orientation = "vertical" <br/> Android: layout_width = "fill_parent" <br/> Android: layout_height = "fill_parent" <br/> <textview <br/> Android: layout_width = "fill_parent" <br/> Android: layout_height = "wrap_content" <br/> Android: TEXT = "@ string/Hello" <br/> <button <br/> Android: id = "@ + ID/B01" <br/> Android: layout_width = "fill_parent" <br/> Android: layout_height = "wrap_content" <br/> <imageview <br/> Android: Id = "@ + ID/iv01" <br/> Android: layout_width = "fill_parent" <br/> Android: layout_height = "wrap_content" <br/> </linearlayout> <br/> 

 

Lesson_01_pic.java:

 

Package COM. yfz; <br/> Import Java. io. filenotfoundexception; <br/> Import android. app. activity; <br/> Import android. content. contentresolver; <br/> Import android. content. intent; <br/> Import android. graphics. bitmap; <br/> Import android. graphics. bitmapfactory; <br/> Import android.net. uri; <br/> Import android. OS. bundle; <br/> Import android. util. log; <br/> Import android. view. view; <br/> Import android. widget. button; <br/> Import android. widget. imageview; <br/> public class lesson_01_pic extends activity {<br/>/** called when the activity is first created. */<br/> @ override <br/> Public void oncreate (bundle savedinstancestate) {<br/> super. oncreate (savedinstancestate); <br/> setcontentview (R. layout. main); </P> <p> button = (button) findviewbyid (R. id. b01); <br/> button. settext ("select image"); <br/> button. setonclicklistener (New button. onclicklistener () {<br/> @ override <br/> Public void onclick (view v) {<br/> intent = new intent (); <br/>/* enable pictures image type to set to image */<br/> intent. settype ("image/*"); <br/>/* use intent. action_get_content: Action */<br/> intent. setaction (intent. action_get_content); <br/>/* returns the current image after obtaining the photo */<br/> startactivityforresult (intent, 1 ); <br/>}</P> <p >}); <br/>}</P> <p> @ override <br/> protected void onactivityresult (INT requestcode, int resultcode, intent data) {<br/> If (resultcode = result_ OK) {<br/> URI uri = data. getdata (); <br/> log. E ("Uri", Uri. tostring (); <br/> contentresolver Cr = This. getcontentresolver (); <br/> try {<br/> Bitmap bitmap = bitmapfactory. decodestream (Cr. openinputstream (URI); <br/> imageview = (imageview) findviewbyid (R. id. iv01); <br/>/* Set bitmap to imageview */<br/> imageview. setimagebitmap (Bitmap); <br/>} catch (filenotfoundexception e) {<br/> log. E ("exception", E. getmessage (), e); <br/>}< br/> super. onactivityresult (requestcode, resultcode, data); <br/>}< br/>} 

 

Okay, that's all.

Related Article

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.