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