This is an improvement after the article "Android pictures, photo albums, Cut and Show"
Although the previous article can solve the picture of the photo cut and display, but found that he has a disadvantage, if the program run alone, seemingly no problem, but to integrate it into the project, found that sometimes on the Xiaomi phone or Samsung phone still fail, the reason may be after the photo caused by memory shortage, So when the photo returned, there is no return data, resulting in a picture display failure, of course, this is a certain probability event.
For this situation, the following changes are made to the following code:
Packagecom.kuiboo.xiaoyao.Activity;Importandroid.app.Activity;Importandroid.content.Intent;ImportAndroid.graphics.Bitmap;Importandroid.graphics.BitmapFactory;ImportAndroid.net.Uri;ImportAndroid.os.Bundle;Importandroid.os.Environment;ImportAndroid.provider.MediaStore;ImportAndroid.util.Log;ImportAndroid.view.View;ImportAndroid.widget.Toast;ImportCOM.KUIBOO.XIAOYAO.R;ImportCom.kuiboo.xiaoyao.util.ActionSheetDialog;ImportCom.kuiboo.xiaoyao.util.CircleImageView;ImportJava.io.BufferedOutputStream;ImportJava.io.ByteArrayOutputStream;ImportJava.io.File;Importjava.io.FileNotFoundException;ImportJava.io.FileOutputStream;ImportJava.io.InputStream;ImportJava.util.Random; Public classEditmineactivityextendsActivity {Private Static FinalString TAG = "Mainactivity"; Private Static Final intTake_small_picture = 2; Private Static Final intCrop_big_picture = 3; Private Static Final intCrop_small_picture = 4; Private Static Final intChoose_small_picture = 6; Private Static FinalString image_file_location = "File:///sdcard/temp.jpg"; PrivateUri Imageuri;//the URI address where the picture is storedCircleimageview iv;//Custom circular images, or use third-party libraries, or basic ImageView can @Overrideprotected voidonCreate (Bundle savedinstancestate) {Super. OnCreate (savedinstancestate); Setcontentview (R.layout.activity_mine_edit); LOG.E ("Executed a few times", "AAA"); IV=(Circleimageview) Findviewbyid (R.id.photoview); Iv.setborderwidth (5); Imageuri=Uri.parse (image_file_location); Iv.setonclicklistener (NewView.onclicklistener () {@Override Public voidOnClick (View v) {NewActionsheetdialog (editmineactivity. This). Builder (). Addsheetitem ("Take pictures", ActionSheetDialog.SheetItemColor.BULE,NewActionsheetdialog.onsheetitemclicklistener () {@Override Public voidOnClick (intwitch) {Opencamera (); }}). Addsheetitem ("Open album", ActionSheetDialog.SheetItemColor.BULE,NewActionsheetdialog.onsheetitemclicklistener () {@Override Public voidOnClick (intwitch) {openpic (); }}). Show (); } }); } //Get album Pictures Private voidOpenpic () {Intent Intent=NewIntent (Intent.action_get_content,NULL); Intent.settype ("Image/*"); Intent.putextra ("Crop", "true"); Intent.putextra ("Aspectx", 2); Intent.putextra ("Aspecty", 1); Intent.putextra ("Outputx", 200); Intent.putextra ("Outputy", 100); Intent.putextra ("Scale",true); Intent.putextra ("Return-data",true); Intent.putextra ("OutputFormat", Bitmap.CompressFormat.JPEG.toString ()); Intent.putextra ("Nofacedetection",true); Startactivityforresult (Intent, choose_small_picture); } //get pictures of small pictures Private voidOpencamera () {if(Imageuri = =NULL) {log.e (TAG,"Picture URI cannot be empty!"); } Intent Intent=NewIntent (mediastore.action_image_capture); Intent.putextra (Mediastore.extra_output, Imageuri); Startactivityforresult (Intent, take_small_picture); } //Cut Private voidCropimageuri (Uri Uri,intOUTPUTX,intOutputy,intRequestcode) {Intent Intent=NewIntent ("Com.android.camera.action.CROP"); Intent.setdataandtype (URI,"Image/*"); Intent.putextra ("Crop", "true"); Intent.putextra ("Aspectx", 2); Intent.putextra ("Aspecty", 1); Intent.putextra ("Outputx", OUTPUTX); Intent.putextra ("Outputy", outputy); Intent.putextra ("Scale",true); Intent.putextra (Mediastore.extra_output, URI); Intent.putextra ("Return-data",false); Intent.putextra ("OutputFormat", Bitmap.CompressFormat.JPEG.toString ()); Intent.putextra ("Nofacedetection",true);//no face recognition.Startactivityforresult (Intent, Requestcode); } PrivateBitmap decodeuriasbitmap (Uri uri) {Bitmap Bitmap=NULL; Try{bitmap=Bitmapfactory.decodestream (Getcontentresolver (). Openinputstream (URI)); } Catch(FileNotFoundException e) {e.printstacktrace (); return NULL; } returnbitmap; } @Overrideprotected voidOnactivityresult (intRequestcode,intResultCode, Intent data) { Super. Onactivityresult (Requestcode, ResultCode, data); if(ResultCode! =ACTIVITY.RESULT_OK) {LOG.E (TAG,"Requestcode =" +Requestcode); LOG.E (TAG,"ResultCode =" +ResultCode); LOG.E (TAG,"Data =" +data); return; }Else{ Switch(requestcode) {//Case Crop_big_picture://Cut Large Image//if (Imageuri! = null) {//Bitmap Bitmap = Decodeuriasbitmap (Imageuri);//Iv.setimagebitmap (bitmap);// }//Break ; CaseTake_small_picture://Take PicturesCropimageuri (Imageuri, 260, 200, crop_small_picture); Break; CaseCrop_small_picture://cut a small image if(Imageuri! =NULL) {Bitmap Bitmap=Decodeuriasbitmap (Imageuri); Iv.setimagebitmap (bitmap); }Else{log.e (TAG,"Crop_small_picture:data =" +data); } Break; CaseChoose_small_picture://Album Selection if(Data! =NULL) {Bitmap Bitmap= Data.getparcelableextra ("Data"); Iv.setimagebitmap (bitmap); }Else{log.e (TAG,"Choose_small_picture:data =" +data); } Break; } } }}
Not on, as in the previous article.
Completely solve problems that can't be displayed after Android takes photos