返回顶部
首页 > 资讯 > 移动开发 >Android实现本地上传图片并设置为圆形头像
  • 808
分享到

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

上传图片图片Android 2022-06-06 09:06:38 808人浏览 泡泡鱼
摘要

先从本地把图片上传到服务器,然后根据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);
        }
       
        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);
                        }
                });
        }
       
        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;
        }
       
        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();
        }
}

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

您可能感兴趣的文章:Android retrofit上传文件实例(包含头像)Android头像上传功能的实现代码(获取头像加剪切)Android个人中心的头像上传,图片编码及截取实例Android实现头像上传功能


--结束END--

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

本文链接: https://lsjlt.com/news/26688.html(转载时请注明来源链接)

有问题或投稿请发送至: 邮箱/279061341@qq.com    QQ/279061341

猜你喜欢
软考高级职称资格查询
编程网,编程工程师的家园,是目前国内优秀的开源技术社区之一,形成了由开源软件库、代码分享、资讯、协作翻译、讨论区和博客等几大频道内容,为IT开发者提供了一个发现、使用、并交流开源技术的平台。
  • 官方手机版

  • 微信公众号

  • 商务合作