欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  移动技术

Android实现本地上传图片并设置为圆形头像

程序员文章站 2022-08-17 18:16:02
先从本地把图片上传到服务器,然后根据url把头像处理成圆形头像。 因为上传图片用到bmob的平台,所以要到bmob()申请密钥。 效果图: 核心代码: 复制代码...

先从本地把图片上传到服务器,然后根据url把头像处理成圆形头像。

因为上传图片用到bmob的平台,所以要到bmob()申请密钥。

效果图:

Android实现本地上传图片并设置为圆形头像

核心代码:

复制代码 代码如下:

public class mainactivity extends activity {
        private imageview iv;
        private string appkey="";                //填写你的application id
        private string path=environment.getexternalstoragedirectory()+"/11.jpeg";        //要上传的图片的路径
        public final int size=2*1024;               
        protected void oncreate(bundle savedinstancestate) {
                super.oncreate(savedinstancestate);
                bmob.initialize(this, appkey);                //初始化bmob sdk
                setcontentview(r.layout.activity_main);
                initview();
        }
        private void initview() {
                // todo auto-generated method stub
                iv=(imageview)findviewbyid(r.id.iv);
        }
        /**
         * 上传文件到bmob后台
         * */
        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("上传成功 ");
                        }
                        @override
                        public void onfailure(int arg0, string arg1) {
                                // todo auto-generated method stub
                                toast("上传失败 "+arg1);
                                log.i("---------", "------error "+arg1);
                        }
                });
        }
        /**
         * 根据url获取bitmap
         * */
        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);
                        //把bitmap转成圆形
                        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();
                }
                //返回圆形bitmap
                return bitmap;
        }
        /**
         * 把bitmap转成圆形
         * */
        public bitmap toroundbitmap(bitmap bitmap){
                int width=bitmap.getwidth();
                int height=bitmap.getheight();
                int r=0;
                //取最短边做边长
                if(width<height){
                        r=width;
                }else{
                        r=height;
                }
                //构建一个bitmap
                bitmap backgroundbm=bitmap.createbitmap(width,height,config.argb_8888);
                //new一个canvas,在backgroundbmp上画图
                canvas canvas=new canvas(backgroundbm);
                paint p=new paint();
                //设置边缘光滑,去掉锯齿
                p.setantialias(true);
                rectf rect=new rectf(0, 0, r, r);
                //通过制定的rect画一个圆角矩形,当圆角x轴方向的半径等于y轴方向的半径时, 
                //且都等于r/2时,画出来的圆角矩形就是圆形
                canvas.drawroundrect(rect, r/2, r/2, p);
                //设置当两个图形相交时的模式,src_in为取src图形相交的部分,多余的将被去掉
                p.setxfermode(new porterduffxfermode(mode.src_in));
                //canvas将bitmap画在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();
        }
}

以上所述就是本文的全部内容了,希望大家能喜欢。