Get a picture resource from an SD card, or take a new picture.
Post Code First
Get Pictures:
Notes: If you take a picture, you can specify the location of the image to save.
Copy Code code as follows:
charsequence[] Items = {"album", "Camera"};
New Alertdialog.builder (This)
. Settitle ("Choose a picture Source")
. Setitems (items, new Onclicklistener () {
public void OnClick (Dialoginterface dialog, int which) {
if (which = = Select_picture) {
Intent Intent = new Intent (intent.action_get_content);
Intent.addcategory (intent.category_openable);
Intent.settype ("image/*");
Startactivityforresult (Intent.createchooser (Intent, "select Picture"), select_picture);
}else{
Intent Intent = new Intent (mediastore.action_image_capture);
Startactivityforresult (Intent, Select_camer);
}
}
})
. Create (). Show ();
process picture, method one, direct processing return picture:
Notes:
1, the on-line has the explanation, the direct processing returns the picture is is compressed by the system, but oneself in the test process does not have the difference;
2, if users continue to retrieve pictures, the current BMP memory must be released, otherwise it will be an error! Bmp.recycle ().
Copy Code code as follows:
protected void Onactivityresult (int requestcode, int resultcode, Intent data) {
Super.onactivityresult (Requestcode, ResultCode, data);
if (ResultCode = = RESULT_OK) {
Select picture
Uri uri = Data.getdata ();
Contentresolver CR = This.getcontentresolver ();
try {
if (BMP!= NULL)//If not released, continue to take pictures, will not have enough memory
Bmp.recycle ();
BMP = Bitmapfactory.decodestream (Cr.openinputstream (URI));
catch (FileNotFoundException e) {
TODO auto-generated Catch block
E.printstacktrace ();
}
System.out.println ("The BMP toString:" + BMP);
Imagesv.setbmp (BMP);
}else{
Toast.maketext (setimageactivity.this, "Please select the picture", Toast.length_short). Show ();
}
}
process picture, method Two, get the address of the picture to be processed again:
Copy Code code as follows:
protected void Onactivityresult (int requestcode, int resultcode, Intent data {
Super.onactivityresult (Requestcode, ResultCode, data);
if (ResultCode = = Result_ok) {
Uri uri = Data.getdata ();
String [] proj={mediastore.images.media.data};
Cursor Cursor = Managedquery (URI,
proj,//Which columns to return
NULL,//WHERE clause; Which rows to R Eturn (all rows)
NULL,//WHERE clause selection arguments (none)
null);//order-by clause (Ascending by name
int column_index = Cursor.getcolumnindexorthrow (MediaStore.Images.Media.DATA);
Cursor.movetofirst ();
String Path = cursor.getstring (Column_index);
BMP = Bitmapfactory.decodefile (path);
System.out.println ("The path is:" + path);
}else{
Toast.maketext (setimageactivity.this, select Picture again, Toast.length_short). Show ();
}
}