Android實現本地上傳圖片並設定為圓形頭像

Android實現本地上傳圖片並設定為圓形頭像
1 Star2 Stars3 Stars4 Stars5 Stars 給文章打分!
Loading...

先從本地把圖片上傳到伺服器,然後根據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, 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裁剪圖片為圓形圖片的實現原理與程式碼Android佈局自定義Shap圓形ImageView可以單獨設定背景與圖片Android中使用Bitmap類將矩形圖片轉為圓形的方法Android實現圓形圖片的兩種方式android圖片處理之讓圖片變成圓形Android中Glide載入圓形圖片和圓角圖片例項程式碼android 實現圓角圖片解決方案Android關於Glide的使用(高斯模糊、載入監聽、圓角圖片)android 設定圓角圖片實現程式碼ndroid實現圓形圖片或者圓角圖片

相關文章

Android 開發 最新文章