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