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