Android--open local image and show path

Source: Internet
Author: User

Background

Code

First on the layout file:

<LinearLayoutxmlns:android= "Http://schemas.android.com/apk/res/android"Xmlns:tools= "Http://schemas.android.com/tools"Android:layout_width= "Match_parent"Android:layout_height= "Match_parent"android:orientation= "vertical"Tools:context=". Mainactivity " >    <ButtonAndroid:id= "@+id/button"Android:layout_width= "Wrap_content"Android:layout_height= "Wrap_content"android:layout_gravity= "Center"Android:text= "@string/hello_world"/>    <ImageViewAndroid:id= "@+id/pic"Android:layout_width= "Wrap_content"Android:layout_height= "Wrap_content"android:layout_gravity= "Center"android:contentdescription= "@string/app_name"/></LinearLayout>

There is no textview here, I finally output the path in System.out.println mode.

Initialization

button =(Button) Findviewbyid (R.id.button); Pic=(ImageView) Findviewbyid (r.id.pic); Button.setonclicklistener (NewView.onclicklistener () {@Override Public voidOnClick (View v) {//TODO Auto-generated method stubsSystem.out.println ("OnClick"); Intent Intent=NewIntent (); Intent.settype ("Image/*");                Intent.setaction (intent.action_get_content); Startactivityforresult (Intent,1); }        });

To overwrite the callback function for Startactivityforresult:

@Overrideprotected voidOnactivityresult (intRequestcode,intResultCode, Intent data) {        //TODO Auto-generated method stubsSystem.out.println (requestcode+ ""); if(requestcode==1)        {            //get the URI of the pictureUri uri =Data.getdata (); //the data provided by the external program Access ContentProvider can be accessed via the Contentresolver interfaceContentresolver CR = This. Getcontentresolver ();            Bitmap Bitmap; //Bitmap BM; //This is a way to read pictures             Try             {                //BM = MediaStore.Images.Media.getBitmap (CR, URI); //Pic.setimagebitmap (BM);Bitmap =Bitmapfactory.decodestream (Cr.openinputstream (URI));                Pic.setimagebitmap (bitmap); System.out.println ("Good");
  //the first way to read a path   // string[] proj = {                MediaStore.Images.Media.DATA};  //seems to be an Android Multimedia database encapsulation interface, specific look at the android document cursor cursor =                 Managedquery (URI, proj, NULL, NULL, NULL);              According to my own understanding, this is the index value of the image that gets the user's choice int column_index = Cursor.getcolumnindexorthrow (MediaStore.Images.Media.DATA);                Move the cursor to the beginning, this is very important, careless can easily cause the crossing cursor.movetofirst ();                Finally get the picture path according to the index value String path = cursor.getstring (Column_index);                   SYSTEM.OUT.PRINTLN (path);  */ 
 //second way to read path cursor cursor                = This. Getcontentresolver (). Query (URI,NULL,NULL,NULL,NULL); intColumn_index =Cursor.getcolumnindexorthrow (MediaStore.Images.Media.DATA);                Cursor.movetofirst (); String Path=cursor.getstring (Column_index);                            SYSTEM.OUT.PRINTLN (path); }             Catch(Exception e) {//TODO Auto-generated catch blockE.printstacktrace (); System.out.println ("Bad"); }                    }        Super. Onactivityresult (Requestcode, ResultCode, data); }

The last path is the path.

I'm the dividing line of the king of the Land Tiger.

Source code: HTTP://PAN.BAIDU.COM/S/1DD1QX01

Openpic.zip

Reprint Please specify source: http://www.cnblogs.com/yydcdut/p/3720594.html

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.