Android obtains the path (URL) of the selected image in the SD card.

Source: Internet
Author: User

The upload function of an image must provide the path of the uploaded image in the SD card, summarize some columns on the Internet, and modify the code. For more information, see, hope to help you

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"/>

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.