Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲
 Android教程網 >> Android技術 >> 關於Android編程 >> Android實現本地上傳圖片並設置為圓形頭像

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

編輯:關於Android編程

先從本地把圖片上傳到服務器,然後根據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();
        }
}

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

  1. 上一頁:
  2. 下一頁:
熱門文章
閱讀排行版
Copyright © Android教程網 All Rights Reserved