后台服务上传头像

去github搜索图片选择器,按照官方配置配置完后实现以下代码:

main-activity.xml里面的代码
xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:gravity="center"
    tools:context="com.example.andy.shangchuantouxiang.MainActivity">

    <ImageView
        android:id="@+id/image"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@mipmap/ic_launcher" />
LinearLayout>

 
  
MainActivity里面的代码:
public class MainActivity extends AppCompatActivity {
    @ViewInject(R.id.image)
    private ImageView image;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        x.view().inject(this);

        image.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(MainActivity.this, PhotoSelectorActivity.class);
                intent.addFlags(Intent.FLAG_ACTIVITY_NO_ANIMATION);
                intent.putExtra("limit", 1);//number是选择图片的数量
                startActivityForResult(intent, 101);
            }
        });
    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        switch (requestCode) {
            case 101:
                if (data != null) {
                    List paths = (List) data.getExtras().getSerializable("photos");//path是选择拍照或者图片的地址数组
                    //处理代码
                    Log.e("onActivityResult", "onActivityResult" + paths);
                    if (paths != null && paths.size() > 0) {
                        //二次采样
                        File im = decodeImage(paths.get(0));
                        //上传
                        upLoad(im);
                    }
                }
                break;
            default:
                break;
        }
        super.onActivityResult(requestCode, resultCode, data);
    }

    private File decodeImage(String s) {
        BitmapFactory.Options options = new BitmapFactory.Options();
        options.inJustDecodeBounds = true;
        BitmapFactory.decodeFile(s,options);
        int inSampleSize = 1;

        int width = options.outWidth;
        int height = options.outHeight;
        //200*200
        while (width / inSampleSize > 200 || height / inSampleSize > 200) {
            inSampleSize *= 2;
        }
        options.inSampleSize = inSampleSize;
        options.inJustDecodeBounds = false;
        Bitmap bitmap = BitmapFactory.decodeFile(s,options);
        String fileName = s.substring(s.lastIndexOf("/") + 1, s.length());
        File file=new File(getExternalCacheDir(),"decode200_"+fileName);

        try {
            FileOutputStream out=new FileOutputStream(file);
            bitmap.compress(Bitmap.CompressFormat.JPEG, 30, out);

            out.flush();
            out.close();
        } catch (Exception e) {
            e.printStackTrace();
        }

        bitmap.recycle();
        return file;

    }

    private void upLoad(File userphoneUrl) {
        RequestParams params=new RequestParams("http://169.254.67.205:8080/1511k/servlet/UploadServlet");
        params.setMultipart(true);
        params.addBodyParameter("file",userphoneUrl);

        x.http().post(params, new Callback.CommonCallback() {
            @Override
            public void onSuccess(String result) {
                Log.e("result",result);
                try {
                    JSONObject json=new JSONObject(result);
                    JSONArray images = json.getJSONArray("images");
                    Glide.with(MainActivity.this).load(images.get(0)).into(image);
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }

            @Override
            public void onError(Throwable ex, boolean isOnCallback) {

            }

            @Override
            public void onCancelled(CancelledException cex) {

            }

            @Override
            public void onFinished() {

            }
        });
    }


}

你可能感兴趣的:(后台服务上传头像)