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