Android Open Gallery

Source: Internet
Author: User

Small demo One, used today, then share to everyone, hope to have a harvest

First on:





Sample source code:

1. Add access to SD card permissions

<uses-permission android:name= "Android.permission.READ_EXTERNAL_STORAGE"/>

2. Source code

Package Com.zengtao.demo;import Java.io.filenotfoundexception;import Android.content.contentresolver;import Android.content.intent;import Android.graphics.bitmap;import Android.graphics.bitmapfactory;import Android.net.uri;import Android.os.bundle;import Android.support.v7.app.actionbaractivity;import Android.view.View ; Import Android.view.view.onclicklistener;import Android.widget.button;import android.widget.imageview;public Class Mainactivity extends Actionbaractivity {private Button bt;private ImageView IV; @Overrideprotected void OnCreate ( Bundle savedinstancestate) {super.oncreate (savedinstancestate); Setcontentview (r.layout.activity_main); bt = (Button ) Findviewbyid (r.id.bt); iv = (ImageView) Findviewbyid (R.ID.IV); Bt.setonclicklistener (new Onclicklistener () {@ overridepublic void OnClick (View v) {Intent Intent = new Intent (); intent.setaction (intent.action_get_content); Intent.settype ("image/*"); Startactivityforresult (intent, 1);});} @Overrideprotected void Onactivityresult (int RequestcoDE, int resultcode, Intent data) {if (Requestcode = = 1) {if (ResultCode = = RESULT_OK) {if (data = null) {URI URI = DATA.G Etdata (); Contentresolver CR = This.getcontentresolver (); try {Bitmap Bitmap = Bitmapfactory.decodestream (Cr.openinputstream (URI )); Iv.setimagebitmap (bitmap);} catch (FileNotFoundException e) {e.printstacktrace ();}}}} Super.onactivityresult (Requestcode, ResultCode, data);}}


The above is to complete the opening of the library, and the selection of the image returned to the current application, of course, there is still a flaw in this, that is, the picture is too large, need to correct, in the galleries to get the picture needs to be processed after a process that is abbreviated after the exception, about this, Believe that other great gods will be mentioned.


Android Open Gallery

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.