Android obtains album images and paths

Source: Internet
Author: User

I am working on a project on the Android platform. I need to use the photo album image to record it by the way, so that I can review it later. Okay, that's the end of the crap. Which of the following is a part?CodeAnd can be modified as needed ~

 

First, obtain the album image:

 

Private final string image_type = "image /*";

Private Final int image_code = 0; // The image_code here is defined by yourself.

 

// Use intent to call the album function provided by the system. startactivityforresult is used to obtain the image selected by the user.

 

Intent getalbum = new intent (intent. action_get_content );

Getalbum. settype (image_type );

Startactivityforresult (getalbum, image_code );

 

// Override onactivityresult to obtain the information you need

 

@ Override

Protected void onactivityresult (INT requestcode, int resultcode, intent data ){

If (resultcode! = Result_ OK) {// The result_ OK here is a constant defined by the system.

Log. E (TAG, "activityresult resultcode error ");

Return;

}

 

Bitmap Bm = NULL;

 

// ExternalProgramYou can access the data provided by contentprovider through the contentresolver interface.

Contentresolver resolver = getcontentresolver ();

 

// The code here is used to determine whether the received activity is the one you want.

If (requestcode = image_code ){

Try {

Uri originaluri = data. getdata (); // obtain the image URI

 

Bm = mediastore. Images. Media. getbitmap (Resolver, originaluri); // The bitmap image is displayed.

 

In the second part, obtain the image path:

 

String [] proj = {mediastore. Images. Media. Data };

 

// It seems to be an android multimedia database encapsulation interface. For details, refer to the android documentation.

Cursor cursor = managedquery (originaluri, proj, null );

// According to my personal understanding, this is the index value of the image selected by the user

Int column_index = cursor. getcolumnindexorthrow (mediastore. Images. Media. data );

// Move the cursor to the beginning. This is very important and it is easy to cross the border accidentally.

Cursor. movetofirst ();

// Obtain the image path based on the index value

String Path = cursor. getstring (column_index );

} Catch (ioexception e ){

Log. E (TAG, E. tostring ());

}

}

}

Address: http://blog.csdn.net/hugh114/article/details/5765332

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.