Recently, I am working on an image upload function. I need to provide the path for uploading images to the SD card. I have read some examples on the Internet. The code is very simple after debugging. The layout file is as follows:
Copy codeThe Code is as follows: <? Xml version = "1.0" encoding = "UTF-8"?>
<LinearLayout xmlns: android = "http://schemas.android.com/apk/res/android"
Android: layout_width = "fill_parent"
Android: layout_height = "fill_parent"
Android: orientation = "vertical">
<Button
Android: id = "@ + id/select"
Android: layout_width = "fill_parent"
Android: layout_height = "wrap_content"
Android: text = "select an image in the SD card"
/>
</LinearLayout>
The java file is as follows:Copy codeThe Code is as follows: package com. lostinai;
Import java. io. IOException;
Import android. app. Activity;
Import android. content. ContentResolver;
Import android. content. Intent;
Import android. database. Cursor;
Import android. graphics. Bitmap;
Import android.net. Uri;
Import android. OS. Bundle;
Import android. provider. MediaStore;
Import android. util. Log;
Import android. view. View;
Import android. widget. Button;
Public class QueryPictureUrlActivity extends Activity {
Private Button select;
Private final String IMAGE_TYPE = "image /*";
Private final int IMAGE_CODE = 0;
Public void onCreate (Bundle savedInstanceState ){
Super. onCreate (savedInstanceState );
SetContentView (R. layout. main );
Select = (Button) findViewById (R. id. select );
Select. setOnClickListener (new View. OnClickListener (){
Public void onClick (View v ){
Intent getAlbum = new Intent (Intent. ACTION_GET_CONTENT );
GetAlbum. setType (IMAGE_TYPE );
StartActivityForResult (getAlbum, IMAGE_CODE );
}
});
}
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;
ContentResolver resolver = getContentResolver ();
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 };
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 );
Cursor. moveToFirst ();
// Obtain the image path based on the index value
String path = cursor. getString (column_index );
Log. e ("Lostinai", path );
} Catch (IOException e ){
Log. e ("Lostinai", e. toString ());
}
}
}
}
Don't forget to add permissions.Copy codeThe Code is as follows: <uses-permission android: name = "android. permission. READ_EXTERNAL_STORAGE"/>
<Uses-permission android: name = "android. permission. MOUNT_UNMOUNT_FILESYSTEMS"/>
<Uses-permission android: name = "android. permission. WRITE_EXTERNAL_STORAGE"/>