Android image Drawing (vi) Get a picture resource such as a local picture or photo photo _android

Source: Internet
Author: User
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 ();
}
}
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.