Android Image drawing--get local picture or photo code

Source: Internet
Author: User

Get a picture resource from an SD card, or take a new picture.
Note: If you take a photo, you can specify the save address for the picture, which is not indicated here.
Get a picture resource from an SD card, or take a new picture.
Post Code First
Get Pictures:
Note: If you take a photo, you can specify the save address for the picture, which is not indicated here.
  

The code is as follows Copy Code
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 ();
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:
Comments:
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 ().

The code is as follows Copy Code

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 ();
}
}
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:

The code is as follows Copy Code

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 return (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, "Please select the picture", Toast.length_short). Show ();
}
}
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 return (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, "Please select the picture", 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.