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

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

概述先从本地把图片上传到服务器,然后根据URL把头像处理成圆形头像。因为上传图片用到bmob的平台,所以要到bmob(http://www.bmob.cn)申请密钥。

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

因为上传图片用到bmob的平台,所以要到bmob(http://www.bmob.cn)申请密钥。

效果图:

核心代码:

复制代码 代码如下:
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,r,r);
                //通过制定的rect画一个圆角矩形,当圆角X轴方向的半径等于Y轴方向的半径时, 
                //且都等于r/2时,画出来的圆角矩形就是圆形
                canvas.drawRoundRect(rect,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();
        }
}

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

总结

以上是内存溢出为你收集整理的Android实现本地上传图片并设置为圆形头像全部内容,希望文章能够帮你解决Android实现本地上传图片并设置为圆形头像所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/web/1142094.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-31
下一篇 2022-05-31

发表评论

登录后才能评论

评论列表(0条)

保存