In the previous article, we introduced how to convert a URI into a string, and this article uses the method of the previous passage. Below we describe how to set the avatar after the image upload to the cloud method, this article based on the services provided by Bmob.
Take a look at the code: (The layout file is still the same as the first two articles, no longer available)
Package Com.example.userphoto;import Java.io.file;import Android.app.activity;import android.content.intent;import Android.database.cursor;import Android.graphics.bitmap;import Android.graphics.drawable.bitmapdrawable;import Android.graphics.drawable.drawable;import Android.net.uri;import Android.os.bundle;import android.os.Environment; Import Android.provider.mediastore;import Android.view.view;import Android.view.view.onclicklistener;import Android.widget.button;import Android.widget.imageview;import Android.widget.toast;import Cn.bmob.v3.Bmob;import Cn.bmob.v3.datatype.bmobfile;import Cn.bmob.v3.listener.uploadfilelistener;public class MainActivity extends Activity implements Onclicklistener {private static final int image_request_code = 0;private static final int camera_reque St_code = 1;private static final int resize_request_code = 2;private static final String image_file_name = "Header.jpg";p R Ivate ImageView mimageheader; @Overrideprotected void OnCreate (Bundle savedinstancestate){super.oncreate (savedinstancestate); Setcontentview (R.layout.activity_main); Bmob.initialize (This, "8f3ffb2658d8a3366a70a0b0ca0b71b2");//Initialize Bmobsetupviews ();} private void Setupviews () {Mimageheader = (ImageView) Findviewbyid (R.id.image_header); final Button selectBtn1 = (Button) Findviewbyid (r.id.btn_selectimage); final Button selectBtn2 = (Button) Findviewbyid (R.id.btn_takephoto); Selectbtn1.setonclicklistener (this); Selectbtn2.setonclicklistener (this);} @Overridepublic void OnClick (View v) {switch (V.getid ()) {case R.id.btn_selectimage:intent galleryintent = new Intent (Int Ent. action_get_content); galleryintent.addcategory (intent.category_openable); Galleryintent.settype ("image/*");// Picture Startactivityforresult (Galleryintent, Image_request_code); Break;case r.id.btn_takephoto:if (isSdcardExisting ()) {Intent cameraintent = new Intent ("Android.media.action.IMAGE_CAPTURE");//Photo Cameraintent.putextra (Mediastore.extra _output, Getimageuri ()); Cameraintent.putextra (mediastore.extra_video_quality, 0); StartaCtivityforresult (Cameraintent, Camera_request_code);} else {Toast.maketext (V.getcontext (), "Please insert SD card", Toast.length_long). Show (); Break;}} @Overrideprotected void Onactivityresult (int requestcode, int resultcode, Intent data) {if (ResultCode! = RESULT_OK) {Retu RN;} else {switch (requestcode) {case Image_request_code:uri originaluri=data.getdata ();//Get Picture Uriresizeimage (Originaluri )///The following method converts the obtained URI to a string type Oh. String []imgs1={mediastore.images.media.data};//Converts the picture URI to a storage path Cursor cursor=this.managedquery (Originaluri, IMGS1, NULL, NULL, NULL); int Index=cursor.getcolumnindexorthrow (MediaStore.Images.Media.DATA); Cursor.movetofirst (); String img_url=cursor.getstring (index); Upload (Img_url);//showtoast (Img_url); Break;case camera_request_code:if (Issdcardexisting ()) {Resizeimage ( Getimageuri ()); String []imgs={mediastore.images.media.data};//Converts the picture URI to the storage path Cursor cursor1=this.managedquery (Getimageuri (), IMGs, NULL, NULL, NULL); int Index1=cursor1.getcolumnindexorthrow (MediaStore.Images.Media.DATA); Cursor1.movetofirst (); String img_url1=cursor1.getstring (index1); upload (IMG_URL1);//showtoast (IMG_URL1);} else {toast.maketext (mainactivity.this, "No memory card found, no photos saved! ", Toast.length_long). Show ();} Break;case resize_request_code:if (data = null) {showresizeimage (data);} Break;}} Super.onactivityresult (Requestcode, ResultCode, data);} private void Showtoast (String msg) {Toast.maketext (this, MSG, Toast.length_short). Show (); Private Boolean issdcardexisting () {//infers if the SD card has a final String state = Environment.getexternalstoragestate (); State.equals (environment.media_mounted)) {return true;} else {return false;}} public void Resizeimage (URI uri) {//Reshape picture size intent Intent = new Intent ("Com.android.camera.action.CROP"); Intent.setdataandtype (URI, "image/*"); Intent.putextra ("Crop", "true");//ability to crop Intent.putextra ("Aspectx", 1); Intent.putextra ("Aspecty", 1), Intent.putextra ("Outputx", "Intent.putextra"; Return-data ", true); Startactivityforresult (Intent, resize_request_code);} Private void Showresizeimage (Intent data) {//Show picture Bundle extras = Data.getextras (); if (extras! = null) {Bitmap photo = extra S.getparcelable ("Data");D rawable drawable = new bitmapdrawable (photo); mimageheader.setimagedrawable (drawable);}} Private Uri Getimageuri () {//Get path return Uri.fromfile (New File (Environment.getexternalstoragedirectory (), Image_file_ NAME));} /** * Upload image * @param imgpath */private void upload (String imgpath) {final bmobfile icon = new Bmobfile (new File (Imgpath)); Con.upload (This, new Uploadfilelistener () {@Overridepublic void onsuccess () {//TODO auto-generated method Stubperson per son = new person ();p Erson.seticon (icon);p erson.save (mainactivity.this); Showtoast ("image upload succeeded");} @Overridepublic void OnProgress (Integer arg0) {//TODO auto-generated method stub} @Overridepublic void OnFailure (int arg0 , String arg1) {//TODO auto-generated method Stubshowtoast ("Picture upload failed:" +ARG1);}});}
JavaBean class:
Package Com.example.userphoto;import Cn.bmob.v3.bmobobject;import Cn.bmob.v3.datatype.bmobfile;public class person Extends Bmobobject {private bmobfile icon;public bmobfile GetIcon () {return icon;} public void SetIcon (bmobfile icon) {This.icon = icon;}}
Execute the instance, and again the failure occurs:
Or a permissions issue:
And then do it again:
Prompt image upload successful. Let's look backstage:
OK, the picture has been uploaded successfully! Isn't it very hanging?
Welcome to reprint, Exchange, praise, attention!
Your support is my power!!!
!
Android Combat Simple Tutorial-the 28th gun (based on bmob to achieve Avatar image settings and network upload Function!) )