Now no matter what app has an avatar, if you see no avatar of the app will feel very strange, before the head is square, and then become a round, I estimate in a few years to get a star shape of the head, below I put the code to change the avatar to write:
<relativelayout xmlns:android= "http://schemas.android.com/apk/res/android" xmlns:tools= "http// Schemas.android.com/tools "android:layout_width=" match_parent "android:layout_height=" Match_parent "Tools:context =". Mainactivity "> <imageview android:id=" @+id/iv_head "android:layout_width=" 200dip "android:l ayout_height= "200dip" android:layout_alignparenttop= "true" android:layout_centerhorizontal= "true" and roid:layout_margintop= "210dip" android:src= "@drawable/ic_launcher"/> <button android:id= "@+id/btn_ Takephoto "android:layout_width=" wrap_content "android:layout_height=" Wrap_content "Android:layout_al Ignparentleft= "true" android:layout_alignparenttop= "true" android:text= "photographed"/> <button Andro Id:id= "@+id/btn_photos" android:layout_width= "wrap_content" android:layout_height= "Wrap_content" Android Oid:layout_alignparentright= "true"Android:layout_alignparenttop= "true" Android:text= "take from album"/></relativelayout>
First I defined a very simple XML layout with two buttons and a imageview to display the Avatar:
Let me take a look at the Mainactivity code:
Package Com.example.changhead;import Java.io.file;import Java.io.filenotfoundexception;import Java.io.fileoutputstream;import Java.io.ioexception;import Android.net.uri;import Android.os.Bundle;import Android.os.environment;import Android.provider.mediastore;import Android.annotation.suppresslint;import Android.app.activity;import Android.content.intent;import Android.graphics.bitmap;import Android.graphics.bitmapfactory;import Android.graphics.drawable.bitmapdrawable;import Android.graphics.drawable.drawable;import Android.view.view;import Android.view.view.onclicklistener;import Android.widget.button;import Android.widget.ImageView; @SuppressLint ("Sdcardpath") public class Mainactivity extends Activity implements Onclicklistener {private ImageView ivhead;//avatar Display private button btntakephoto;//Photo Private button btnphotos;//album private Bitmap head;//avatar bitmapprivate static String path= "/sdcard/myhead/";//SD path @overrideprotected void OnCreate (Bundle savedinstancestate) {super.oncreate (SavedinstanCestate); Setcontentview (R.layout.activity_main); Initview ();} private void Initview () {//Initialize control Btnphotos = (Button) Findviewbyid (r.id.btn_photos); Btntakephoto = (Button) Findviewbyid (R.id.btn_takephoto); Btnphotos.setonclicklistener (this), Btntakephoto.setonclicklistener (this); IvHead = ( ImageView) Findviewbyid (R.id.iv_head); Bitmap BT = bitmapfactory.decodefile (path + "head.jpg");//Find the Avatar from SD, convert it to BITMAPIF (bt!=null) {@SuppressWarnings (" Deprecation ") drawable drawable = new bitmapdrawable (BT);//Convert to Drawableivhead.setimagedrawable (drawable);} else{/** * If there is no SD inside you need to take the avatar from the server, retrieve the avatar and save in SD * * *}} @Overridepublic void OnClick (View v) {switch (V.getid ()) {case R.ID.BTN _photos://take photos from the album Intent intent1 = new Intent (Intent.action_pick, null); Intent1.setdataandtype ( MediaStore.Images.Media.EXTERNAL_CONTENT_URI, "image/*"); Startactivityforresult (intent1, 1); Break;case R.id.btn_ takephoto://call camera to take pictures Intent Intent2 = new Intent (mediastore.action_image_capture); Intent2.putextra (Mediastore.extra_ OUTPUT, Uri.fromfile (New FiLe (Environment.getexternalstoragedirectory (), "head.jpg")); Startactivityforresult (Intent2, 2);// Open break;default:break with Forresult;}} protected void Onactivityresult (int requestcode, int resultcode, Intent data) {switch (requestcode) {case 1:if (ResultCode = = Result_ok) {Cropphoto (Data.getdata ());//Crop Picture}break;case 2:if (ResultCode = = RESULT_OK) {File temp = new file (environme Nt.getexternalstoragedirectory () + "/head.jpg"); Cropphoto (Uri.fromfile (temp));//Crop Picture}break;case 3:if (data! = NULL) { Bundle extras = Data.getextras (), head = extras.getparcelable ("Data"), if (head!=null) {/** * Upload server Code */setpictoview (head) ;//Save in SD card Ivhead.setimagebitmap (head);//show it with ImageView}}break;default:break;} Super.onactivityresult (Requestcode, ResultCode, data);};/ * * Call system clipping * @param uri */public void Cropphoto (Uri uri) {Intent Intent = new Intent ("Com.android.camera.action.CROP"); Ntent.setdataandtype (URI, "image/*"); Intent.putextra ("Crop", "true"); Aspectx Aspecty is a proportional Intent.putextra ("Aspectx", 1) of the wide height; intent.putExtra ("Aspecty", 1);//Outputx outputy is a cropped image with a wide height of Intent.putextra ("Outputx", and Intent.putextra); ("Outputy", 150); Intent.putextra ("Return-data", true); Startactivityforresult (Intent, 3);} private void Setpictoview (Bitmap mbitmap) {String sdstatus = Environment.getexternalstoragestate (); if (!sdstatus.equals (environment.media_mounted)) {//detects if SD is available for return; } FileOutputStream b = null; File File = new file (path); File.mkdirs ()//Create folder string FileName =path + "head.jpg";//Picture name try {b = new FileOutputStream (fi Lename) mbitmap.compress (Bitmap.CompressFormat.JPEG, b);//write data to file} catch (FileNotFoundException e) { E.printstacktrace ();} Finally {try {//Close stream B.flush (); B.close ();} catch (IOException e) {e.printstacktrace ();}}}}
Do not forget to add permissions:
<uses-permission android:name= "Android.permission.CAMERA"/><uses-permission android:name= " Android.permission.WRITE_EXTERNAL_STORAGE "/><uses-permission android:name=" android.permission.MOUNT_ Unmount_filesystems "/>
Implementation of "Android" Avatar replacement