A picture upload function needs to provide upload pictures in the SD card path, summed up some of the online columns, modified a bit, the code is very simple, interested friends can refer to ha, I hope to help you
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 the picture
BM = MediaStore.Images.Media.getBitmap (resolver, Originaluri); appear to bitmap picture
//Here begins the second part, gets the path of the picture:
string[] proj = {MediaStore.Images.Media.DATA};
Cursor Cursor = Managedquery (Originaluri, proj, NULL, NULL, NULL);
//On my own understanding this is the index value of the user-selected picture
int column_index = Cursor.getcolumnindexorthrow (MediaStore.Images.Media.DATA);
Cursor.movetofirst ();
//finally get Picture path based on 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 "/>