First from the local image upload to the server, and then according to the URL of the avatar processing into a round head.
Because the upload image is used to the Bmob platform, the Bmob (http://www.bmob.cn) Request key.
Effect Chart:
Core code:
Copy Code code as follows:
public class Mainactivity extends activity {
Private ImageView IV;
Private String appkey= ""; Fill in your application ID
Private String Path=environment.getexternalstoragedirectory () + "/11.jpeg"; The path of the picture to upload
public final int size=2*1024;
protected void OnCreate (Bundle savedinstancestate) {
Super.oncreate (savedinstancestate);
Bmob.initialize (this, appkey); Initializing the Bmob SDK
Setcontentview (R.layout.activity_main);
Initview ();
}
private void Initview () {
TODO auto-generated Method Stub
iv= (ImageView) Findviewbyid (R.ID.IV);
}
/**
* Upload files to Bmob backstage
* */
public void upload (View v) {
Final Bmobfile file=new bmobfile (new file (path));
File.uploadblock (This, new Uploadfilelistener () {
@Override
public void onsuccess () {
TODO auto-generated Method Stub
Person P=new person ();
P.seturl (File.getfileurl (mainactivity.this));
P.save (Mainactivity.this);
MyTask task=new MyTask ();
Task.execute (File.getfileurl (mainactivity.this));
Toast ("Upload success");
}
@Override
public void onfailure (int arg0, String arg1) {
TODO auto-generated Method Stub
Toast ("Upload failed" +arg1);
LOG.I ("---------", "------error" +ARG1);
}
});
}
/**
* Get bitmap based on URL
* */
Public Bitmap gethttpbitmap (String URL) {
Bitmap Bitmap=null;
URL Myurl;
try {
Myurl=new url (URL);
HttpURLConnection conn= (httpurlconnection) myurl.openconnection ();
Conn.setconnecttimeout (5000);
Conn.connect ();
InputStream Is=conn.getinputstream ();
Bitmap=bitmapfactory.decodestream (IS);
Turn the bitmap into a circle
Bitmap=toroundbitmap (bitmap);
Is.close ();
catch (Malformedurlexception e) {
TODO auto-generated Catch block
E.printstacktrace ();
catch (IOException e) {
TODO auto-generated Catch block
E.printstacktrace ();
}
Return round bitmap
return bitmap;
}
/**
* Turn the bitmap into a circle
* */
Public Bitmap Toroundbitmap (Bitmap Bitmap) {
int Width=bitmap.getwidth ();
int Height=bitmap.getheight ();
int r=0;
Take the shortest side and make the side length
if (widthR=width;
}else{
R=height;
}
Build a bitmap
Bitmap Backgroundbm=bitmap.createbitmap (width,height,config.argb_8888);
New one canvas, drawing on backgroundbmp
Canvas canvas=new Canvas (BACKGROUNDBM);
Paint p=new Paint ();
Set edges smooth, remove sawtooth
P.setantialias (TRUE);
RECTF rect=new RECTF (0, 0, R, R);
Draw a rounded rectangle by drawing the rect, and when the radius of the x-axis of the fillet equals the radius of the y-axis direction,
and all equal to R/2, the drawn rounded rectangle is rounded.
Canvas.drawroundrect (Rect, R/2, R/2, p);
Sets the pattern at the intersection of two shapes, src_in the section that intersects the SRC graphic, and the extra will be removed
P.setxfermode (New Porterduffxfermode (mode.src_in));
Canvas bitmap painted on backgroundbmp.
Canvas.drawbitmap (bitmap, NULL, RECT, p);
return backgroundbm;
}
Class MyTask extends Asynctask<string, String, bitmap>{
@Override
Protected Bitmap doinbackground (String ... arg0) {
TODO auto-generated Method Stub
String Url=arg0[0];
Bitmap bm=gethttpbitmap (URL);
return BM;
}
@Override
protected void OnPostExecute (Bitmap result) {
TODO auto-generated Method Stub
Iv.setimagebitmap (result);
}
}
public void Toast (String msg) {
Toast.maketext (This, MSG, Toast.length_short). Show ();
}
}
The above mentioned is the entire content of this article, I hope you can enjoy.