Program Analysis:
Two main parts
(1) Take pictures first, then cut them and show them at last. (2) Select a photo from the album to cut it and show it
Code refers to the first line of code, the Book Code Runtime (2) a problem, cannot cut the picture and cannot load the picture
The Mainactivity.java code is as follows:
1 Packagecom.example.lynn.choosepictest;2 3 Importandroid.content.Intent;4 ImportAndroid.graphics.Bitmap;5 Importandroid.graphics.BitmapFactory;6 ImportAndroid.net.Uri;7 Importandroid.os.Environment;8 ImportAndroid.provider.MediaStore;9 Importandroid.support.v7.app.AppCompatActivity;Ten ImportAndroid.os.Bundle; One ImportAndroid.util.Log; A ImportAndroid.view.View; - ImportAndroid.widget.Button; - ImportAndroid.widget.ImageView; the - ImportJava.io.File; - Importjava.io.FileNotFoundException; - + Public classMainactivityextendsappcompatactivity { - + Public Static Final intTake_photo = 1; A Public Static Final intCrop_photo = 2; at PrivateButton takepic; - PrivateButton choosebtn; - PrivateImageView picture; - PrivateUri Imageuri; - - @Override in protected voidonCreate (Bundle savedinstancestate) { - Super. OnCreate (savedinstancestate); to Setcontentview (r.layout.activity_main); +Takepic =(Button) Findviewbyid (r.id.take_pic); -CHOOSEBTN =(Button) Findviewbyid (r.id.choose_from_album); the *Choosebtn.setonclicklistener (NewView.onclicklistener () { $ @OverridePanax Notoginseng Public voidOnClick (View v) { -File Outputimage =NewFile (environment.getexternalstoragedirectory () the, "Output_image.jpg"); + Try { A if(Outputimage.exists ()) { the Outputimage.delete (); + } - outputimage.createnewfile (); $}Catch(Exception e) { $ e.printstacktrace (); - } - theImageuri =Uri.fromfile (outputimage); -Intent Intent =NewIntent ("Android.intent.action.PICK");WuyiIntent.settype ("image/*"); theIntent.putextra ("Crop",true); -Intent.putextra ("scale",true); Wu //Intent.putextra (Mediastore.extra_output,imageuri); - //This code in the book does not run, indicating that the selected picture Imageuri not pass through, or that is empty! AboutStartactivityforresult (Intent, Take_photo);//parameter Transmission Take_photo $ } - }); - -Takepic.setonclicklistener (NewView.onclicklistener () { A @Override + Public voidOnClick (View v) { the -File Outputimage =NewFile (Environment.getexternalstoragedirectory (), "Tempimage.jpg"); $ Try { the if(Outputimage.exists ()) { the Outputimage.delete (); the } the outputimage.createnewfile (); -}Catch(Exception e) { in e.printstacktrace (); the } theImageuri =Uri.fromfile (outputimage); AboutIntent Intent =NewIntent ("Android.media.action.IMAGE_CAPTURE"); the Intent.putextra (Mediastore.extra_output, Imageuri); theStartactivityforresult (Intent, Take_photo);//Start the camera program the + } - }); the }Bayi the @Override the protected voidOnactivityresult (intRequestcode,intResultCode, Intent data) { - - Switch(requestcode) { the //cropping work the CaseTake_photo: the the if(ResultCode = =RESULT_OK) { - if(Data! =NULL) { theImageuri = Data.getdata ();//The key to this sentence is that after you select the picture, the Imageuri does not take a value, so here 's the value the } theLOG.E ("TAG", "Take_photo");94Intent intent1 =NewIntent ("Com.android.camera.action.CROP"); theIntent1.setdataandtype (Imageuri, "image/*"); theIntent1.putextra ("scale",true); the Intent1.putextra (Mediastore.extra_output, Imageuri);98 Startactivityforresult (Intent1, Crop_photo); About } - Break;101 102 103 //Show Pictures104 CaseCrop_photo: the if(ResultCode = =RESULT_OK) {106 Try {107Bitmap Bitmap =Bitmapfactory.decodestream (108 Getcontentresolver ()109 . Openinputstream (Imageuri)); the Picture.setimagebitmap (bitmap);111}Catch(FileNotFoundException e) { the e.printstacktrace ();113 } the } the Break; the default:117 Break;118 }119 } -}
View Code
About resolving images that cannot be clipped after selecting photos from an album and cannot load pictures