使用PhotoPicker完成上传头像

1,将jar复制粘贴在libs

2,在build.gradle文件中添加

    compile files('libs/xUtils-1.9.6.jar')
    compile files('libs/org.apache.http.legacy.jar')

3.在项目中新建一个MyApplication继承Application
重写onCreate()

  x.Ext.init(this);

4.在AndroidMainfest文件中的application标签内加上
android:name=".MyApplication"

5.拿到手机一张照片

                PhotoPicker.builder()
                        .setPhotoCount(1)
                        .start(this);

6,上传头像核心代码

private void doUpload(){
        RequestParams params=new RequestParams("http://172.16.14.100:8080/CarMarket/userAction_upLoadPhoto.action");
    params.addBodyParameter("id","44");
    params.addBodyParameter("headPhoto",new File(selectedPhotos.get(0)),"image/*");

    x.http().request(HttpMethod.POST,params,new Callback.CommonCallback(){


        @Override
        public void onSuccess(String s) {
            Log.e("yz","onSuccess="+s);
        }

        @Override
        public void onError(Throwable throwable, boolean b) {
            Log.e("yz","onError="+throwable.toString());
        }

        @Override
        public void onCancelled(CancelledException e) {

        }

        @Override
        public void onFinished() {

        }
    });
}

你可能感兴趣的:(Util类)