SoFunction
Updated on 2025-03-11

Android implements local uploading of pictures and setting them as circular avatars


public class MainActivity extends Activity {
        private ImageView iv;
private String appKey="";
private String path=()+"/";          //The path of the image to be uploaded
        public final int SIZE=2*1024;               
        protected void onCreate(Bundle savedInstanceState) {
                (savedInstanceState);
(this, appKey);                                                           �
                setContentView(.activity_main);
                initView();
        }
        private void initView() {
                // TODO Auto-generated method stub
                iv=(ImageView)findViewById();
        }
        /**
* Upload files to bmob background
         * */
        public void upload(View v){
                final BmobFile file=new BmobFile(new File(path));
                (this, new UploadFileListener() {
                        @Override
                        public void onSuccess() {
                                // TODO Auto-generated method stub
                                Person p=new Person();
                                (());
                                ();
                                MyTask task=new MyTask();
                                (());
toast("Uploaded successfully");
                        }
                        @Override
                        public void onFailure(int arg0, String arg1) {
                                // TODO Auto-generated method stub
toast("Upload failed"+arg1);
                                ("---------", "------error "+arg1);
                        }
                });
        }
        /**
* Get Bitmap based on URL
         * */
        public Bitmap getHttpBitmap(String url){
                Bitmap bitmap=null;
                URL myUrl;
                try {
                        myUrl=new URL(url);
                        HttpURLConnection conn=(HttpURLConnection)();
                        (5000);
                        ();
                        InputStream is=();
                        bitmap=(is);
//Convert bitmap into a circle
                        bitmap=toRoundBitmap(bitmap);
                        ();
                } catch (MalformedURLException e) {
                        // TODO Auto-generated catch block
                        ();
                } catch (IOException e) {
                        // TODO Auto-generated catch block
                        ();
                }
//Return to the circular bitmap
                return bitmap;
        }
        /**
* Convert bitmap into a circle
         * */
        public Bitmap toRoundBitmap(Bitmap bitmap){
                int width=();
                int height=();
                int r=0;
//Please the shortest edge as the length
                if(width<height){
                        r=width;
                }else{
                        r=height;
                }
//Build a bitmap
                Bitmap backgroundBm=(width,height,Config.ARGB_8888);
//new a Canvas, draw a picture on backgroundBmp
                Canvas canvas=new Canvas(backgroundBm);
                Paint p=new Paint();
//Set the edges smoothly and remove the serrated teeth
                (true);
                RectF rect=new RectF(0, 0, r, r);
//Draw a rounded rectangle through the established rect, when the radius of the X-axis direction of the rounded corner is equal to the radius of the Y-axis direction,
// When they are all equal to r/2, the rounded rectangle drawn is a circle
                (rect, r/2, r/2, p);
//Set the mode when two figures intersect. SRC_IN is the part where the SRC figure intersects, and the excess will be removed.
                (new PorterDuffXfermode(Mode.SRC_IN));
//canvas draw bitmap on backgroundBmp
                (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
                        (result);
                }
        }
        public void toast(String msg){
                (this, msg, Toast.LENGTH_SHORT).show();
        }
}