Android get an example of the path (URL) of a selected picture in an SD card _android

Source: Internet
Author: User
Recently in doing a picture upload function, you need to provide upload pictures in the SD card path, on the Internet to see some examples, change debugging success, the code is very simple. The layout files are as follows:
Copy Code code 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= "Please select picture in SD card"
/>
</LinearLayout>

The Java files are as follows:
Copy Code code 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) {//Here RESULT_OK is the system customization of a constant
LOG.E (TAG, "Activityresult resultcode error");
Return
}
Bitmap BM = NULL;
Contentresolver resolver = Getcontentresolver ();
if (Requestcode = = Image_code) {
try {
Uri Originaluri = Data.getdata (); Get the URI of a picture
BM = MediaStore.Images.Media.getBitmap (resolver, Originaluri); appear to bitmap pictures
Here we start with the second part, get the path to the picture:
String[] proj = {MediaStore.Images.Media.DATA};
Cursor Cursor = Managedquery (Originaluri, proj, NULL, NULL, NULL);
As I understand it, this is the index value of the user-selected picture.
int column_index = Cursor.getcolumnindexorthrow (MediaStore.Images.Media.DATA);
Cursor.movetofirst ();
Finally get the picture path based on the index value
String path = cursor.getstring (Column_index);
LOG.E ("Lostinai", Path);

}catch (IOException e) {

LOG.E ("Lostinai", e.tostring ());

}

}
}
}

Finally, don't forget to add permission.
Copy Code code 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.