Preamble, version compatibility issues are mainly due to errors caused by the format of the URI after 4.4 and 4.4
Info Android 4.4 Select a picture from the gallery, get the picture path and crop
1. Take photos and select pictures ① Select pictures
1 New Intent (intent.action_get_content); 2 Intent.settype ("image/*"); 3 Startactivityforresult (Intent, Gallery_request_code);
② photo
1 New Intent (mediastore.action_image_capture); 2 Startactivityforresult (Intent, Camera_request_code);
2. Get the value from the system
1 Tag Character2 Private Static intCamera_request_code = 1;3 Private Static intGallery_request_code = 2;4 Private Static intCrop_request_code = 3;5 6 7 8 @Override9 protected voidOnactivityresult (intRequestcode,intResultCode, Intent data) {Ten if(Requestcode = =Camera_request_code) { One if(Data = =NULL) { A return; -}Else{//Take Pictures -Bundle Extras =Data.getextras (); the if(Extras! =NULL) { -Bitmap BM = extras.getparcelable ("Data"); -Uri uri =Savebitmap (BM); - startimagezoom (URI); + } - } +}Else if(Requestcode = =Gallery_request_code) { A if(Data = =NULL) {//albums at return; - } - URI Uri; -URI =Data.getdata (); -Uri Fileuri =Converturi (URI); - Startimagezoom (Fileuri); in}Else if(Requestcode = =Crop_request_code) { - if(Data = =NULL) { to return; +}//the cropped picture -Bundle Extras =Data.getextras (); the if(Extras = =NULL) { * return; $ }Panax NotoginsengBitmap BM = extras.getparcelable ("Data"); - Showimageview (BM); the } +}
3. After the picture is selected, the URL is converted to stream and saved.
PrivateUri Converturi (Uri uri) {InputStream is=NULL; Try{ is=getcontentresolver (). Openinputstream (URI); Bitmap Bitmap=Bitmapfactory.decodestream (IS); Is.close (); returnSavebitmap (bitmap); } Catch(FileNotFoundException e) {e.printstacktrace (); return NULL; } Catch(IOException e) {e.printstacktrace (); return NULL; } }
4. Save the picture and remember to add permission
1 PrivateUri Savebitmap (Bitmap BM) {2File TmpDir =NewFile (environment.getexternalstoragedirectory ()3+ "/xiaoxin");4 if(!tmpdir.exists ()) {5 Tmpdir.mkdir ();6 }7File img =NewFile (Tmpdir.getabsolutepath () + "Love.png");8 Try {9FileOutputStream fos =NewFileOutputStream (IMG);TenBm.compress (Bitmap.CompressFormat.PNG, 85, FOS); One Fos.flush (); A fos.close (); -Toast.maketext (mainactivity. This, "succeeded.", Toast.length_short). Show (); - returnuri.fromfile (IMG); the}Catch(FileNotFoundException e) { -Toast.maketext (mainactivity. This, "failed.", Toast.length_short). Show (); - e.printstacktrace (); - return NULL; +}Catch(IOException e) { - e.printstacktrace (); +Toast.maketext (mainactivity. This, "failed.", Toast.length_short). Show (); A return NULL; at } - -}
5. Crop the picture
1 /**2 * Crop Picture3 * 4 * @paramURI5 */6 Private voidstartimagezoom (Uri uri) {7Intent Intent =NewIntent ("Com.android.camera.action.CROP");8Intent.setdataandtype (URI, "image/*");9Intent.putextra ("Crop", "true");TenIntent.putextra ("Aspectx", 1); OneIntent.putextra ("Aspecty", 1); AIntent.putextra ("Outputx", 150); -Intent.putextra ("Outputy", 150); -Intent.putextra ("Return-data",true); the Startactivityforresult (Intent, crop_request_code); -}
Android Select Picture trim take photo compatible with all versions of code