First on the code:
Packagecom.create.intenttest;Importandroid.content.Intent;Importandroid.content.SharedPreferences;ImportAndroid.graphics.Bitmap;ImportAndroid.net.Uri;ImportAndroid.os.Bundle;Importandroid.os.Environment;ImportAndroid.provider.MediaStore;Importandroid.support.v7.app.AppCompatActivity;ImportAndroid.util.Log;ImportAndroid.view.View;ImportAndroid.widget.Button;ImportAndroid.widget.ImageView;ImportJava.io.File; Public classMainactivityextendsappcompatactivity {PrivateSharedpreferences sp; @Overrideprotected voidonCreate (Bundle savedinstancestate) {Super. OnCreate (savedinstancestate); Setcontentview (R.layout.activity_main); SP= getsharedpreferences ("config", mode_private); Button Takephoto=(Button) Findviewbyid (R.ID.BTN); Button Selectphoto=(Button) Findviewbyid (R.ID.BTN2); Takephoto.setonclicklistener (NewView.onclicklistener () {@Override Public voidOnClick (View v) {//Calling camera logicIntent takeintent =NewIntent (mediastore.action_image_capture); //The following sentence specifies the path of the photo store after the camera is called//Note that when you assign a path to a photo, the data returned in the Onactivityresult () method is null.//consider saving the path you set to the SP, removing it from the SP when setting the photo, using Imageview.setimagebitmap (sp.getstring ("Photopath"))//of course, if you shoot and get is in the same activity the path is set directly to the member variable as well. No SPS required. //Takeintent.putextra (Mediastore.extra_output,//Uri.fromfile (New File (Environment.getexternalstoragedirectory (), Constant.photoname ));//Sp.edit (). putstring (Constant.take_photo_path, environment.getexternalstoragedirectory () + "/" + Const Ant. Photoname). commit ();Startactivityforresult (takeintent, Constant.takephoto_code); } }); Selectphoto.setonclicklistener (NewView.onclicklistener () {@Override Public voidOnClick (View v) {Intent Intent=NewIntent (intent.action_get_content);//action_open_documentintent.addcategory (intent.category_openable); Intent.settype ("Image/jpeg"); Startactivityforresult (Intent, constant.select_pic); } }); } @Override Public voidOnactivityresult (intRequestcode,intResultCode, Intent data) {LOG.E ("Data", Data + "----------------"); ImageView ImageView=(ImageView) Findviewbyid (r.id.image); Switch(requestcode) { CaseConstant.takephoto_code://Camera Back if(Data! =NULL) { if(Data.hasextra ("Data") {Bitmap thumbnail= Data.getparcelableextra ("Data"); Imageview.setimagebitmap (thumbnail); } } CaseConstant.select_pic://album return if(Data! =NULL) {URI Uri=Data.getdata (); Imageview.setimageuri (URI); } } }}
The following blog post is quite thorough, but the comment inside the Onactivityresult () method seems a bit problematic:
Http://www.cnblogs.com/mengdd/archive/2013/03/31/2991932.html
//
//
Intent
Toast.maketext (This, "Image saved to:\n" + Data.getdata (), Toast.length_long). Show ();
do not understand the author above the code to put in if (data! =null) because data is not equal to NULL, it indicates that no path is specified when the camera is opened, so it may be the author's wrong operation. Should be put in else{}
if(Data! =NULL) { //when no specific storage path is specifiedlog.d (Log_tag,"Data is not null and file on default position."); //when the storage path is specified (Intent.putextra (Mediastore.extra_output,fileuri);)//Image captured and saved to Fileuri specified in the//IntentToast.maketext ( This, "Image saved to:\n" +data.getdata (), Toast.length_long). Show (); if(Data.hasextra ("Data") {Bitmap thumbnail= Data.getparcelableextra ("Data"); Imageview.setimagebitmap (thumbnail); } } Else{log.d (Log_tag,"Data is null, file saved on target position."); //If There is no thumbnail image data, the image//'ll has been stored in the target output URI.
Call system to take pictures and select photos in System Gallery to set avatar