According to the test, the code in the book does not run properly on its 5.1 version of Huawei's mobile phone. The images that are called are not displayed correctly and appear normal on the company's 4.x version of the phone.
The code is as follows
Packageorg.quentin.choosepictest;ImportJava.io.File;Importjava.io.FileNotFoundException;Importjava.io.IOException;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.view.Menu;ImportAndroid.view.MenuItem;ImportAndroid.view.View;ImportAndroid.view.View.OnClickListener;ImportAndroid.widget.Button;ImportAndroid.widget.ImageView;/** After testing, it is no problem to find that the code runs on the company's test machine. But in their own 5.1 mobile phone test results are not normal. * This is really strange. When learning a book, there is still a relatively stable version of the better. When you get started, you can find new features by looking up documents by yourself * It's possible to do more with real project development*/ Public classMainactivityextendsActivity { Public Static Final intTake_photo = 1; Public Static Final intCrop_photo = 2; PrivateButton Takephoto; PrivateImageView picture; PrivateUri Imageuri;// - @Overrideprotected voidonCreate (Bundle savedinstancestate) {Super. OnCreate (savedinstancestate); Setcontentview (R.layout.activity_main); Takephoto=(Button) Findviewbyid (R.id.take_photo); picture=(ImageView) Findviewbyid (r.id.picture);//takephoto.settext ("Hello Thoto");Takephoto.setonclicklistener (NewOnclicklistener () {@Override Public voidOnClick (View v) {//TODO auto-generated Method StubFile Outputimage =NewFile (Environment.getexternalstoragedirectory (), "Tempimage.jpg"); Try{ if(Outputimage.exists ()) {outputimage.delete (); } outputimage.createnewfile (); }Catch(IOException e) {e.printstacktrace (); } Imageuri=Uri.fromfile (outputimage); Intent Intent=NewIntent (mediastore.action_image_capture); Intent.putextra (Mediastore.extra_output, Imageuri); Startactivityforresult (Intent, Take_photo); } }); } @Overrideprotected voidOnactivityresult (intRequestcode,intResultCode, Intent data) { //TODO auto-generated Method Stub Super. Onactivityresult (Requestcode, ResultCode, data); Switch(requestcode) { CaseTake_photo:if(ResultCode = =RESULT_OK) {Intent Intent=NewIntent ("Com.android.camera.action.CROP"); Intent.setdataandtype (Imageuri,"Image/*"); Intent.putextra ("Scale", 1); Intent.putextra (Mediastore.extra_output, Imageuri); Startactivityforresult (Intent, Crop_photo); } Break; CaseCrop_photo:if(ResultCode = =RESULT_OK) { Try{Bitmap Bitmap=Bitmapfactory.decodestream (Getcontentresolver (). Openinputstream (Imageuri) ); Picture.setimagebitmap (bitmap); }Catch(FileNotFoundException e) {e.printstacktrace (); } } Break; default: Break; }} @Override Public BooleanOncreateoptionsmenu (Menu menu) {//inflate the menu; This adds items to the action bar if it is present.getmenuinflater (). Inflate (R.menu.main, menu); return true; } @Override Public Booleanonoptionsitemselected (MenuItem item) {//Handle Action Bar item clicks here. The Action Bar would//automatically handle clicks on the Home/up button, so long//As you specify a the parent activity in Androidmanifest.xml. intID =Item.getitemid (); if(id = =r.id.action_settings) { return true; } return Super. onoptionsitemselected (item); }}
Reasons for the moment are not quite clear, with Log printing and various conditions of debugging, also not normal. At present, familiar with the code phase, temporarily do not drill down, mark a bit, later in-depth study it.
Android-the first line of code calls the camera