Retrofit的POST方法上传头像

网络权限

    //上传头像
    private static final int CHOOSE_PICTURE=0;
    private static final int TAKE_PICTURE=1;
    private static final int CROP_SMALL_PICTURE=2;
    private static Uri tempUri;


 
  
按钮的点击事件
    /**
     * 上传头像
     * @param view
     */
    @Override
    public void onClick(View view) {
        AlertDialog.Builder builder=new AlertDialog.Builder(this);
        builder.setTitle("设置头像");
        String[] items={"选择本地照片","拍照"};
        builder.setNegativeButton("取消",null);
        builder.setItems(items, new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialogInterface, int i) {
                switch (i){
                    case CHOOSE_PICTURE://选择本地图片
                        Intent intent=new Intent(Intent.ACTION_PICK, MediaStore.Images.Media.EXTERNAL_CONTENT_URI);;
                        intent.setDataAndType(MediaStore.Images.Media.EXTERNAL_CONTENT_URI,"image/*");
                        startActivityForResult(intent,CHOOSE_PICTURE);
                        break;
                    case TAKE_PICTURE://拍照
                        Intent intent1=new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
                        tempUri=Uri.fromFile(new File(Environment.getExternalStorageDirectory(),"image.jpg"));
                        intent1.putExtra(MediaStore.EXTRA_OUTPUT,tempUri);
                        startActivityForResult(intent1,TAKE_PICTURE);
                        break;
                }
            }
        });
        builder.create().show();
    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        switch (requestCode){
            case TAKE_PICTURE:
                startPhotoZoom(tempUri);
                break;
            case CHOOSE_PICTURE:
                startPhotoZoom(data.getData());
                break;
            case CROP_SMALL_PICTURE:
                if(data!=null){
                    setImageToView(data);
                }
                break;
        }
    }
    private void startPhotoZoom(Uri uri) {
        if(uri==null){
            Log.i("tag","The uri is not exist");
        }
        tempUri=uri;
        Intent in=new Intent("com.android.camera.action.CROP");
        in.setDataAndType(uri,"image/*");
        //设置裁剪
        in.putExtra("crop","true");
        in.putExtra("aspectX",1);
        in.putExtra("aspectY",1);
        //宽高
        in.putExtra("outputX",150);
        in.putExtra("outputY",150);
        in.putExtra("return-data",true);
        startActivityForResult(in,CROP_SMALL_PICTURE);
    }

    private void setImageToView(Intent data) {
        Bundle extras=data.getExtras();
        if(extras!=null){
            Bitmap photo=extras.getParcelable("data");
            iv_img.setImageBitmap(photo);
            saveImage(photo);
            File file=new File(this.getCacheDir()+"/aa.jpg");
            //使用Retrofit上传头像
            Retrofit retrofitHead=new Retrofit.Builder()
                    .baseUrl("https://www.zhaoapi.cn/")
                    .addConverterFactory(GsonConverterFactory.create())
                    .build();

            RequestBody requestBody=RequestBody.create(MediaType.parse("image/png"),file);
            MultipartBody.Part multipartBody=MultipartBody.Part.createFormData("file",file.getName(),requestBody);
            BlogServiceHead head=retrofitHead.create(BlogServiceHead.class);
            Call userCall=head.getHead("148",multipartBody);
            userCall.enqueue(new Callback() {
                @Override
                public void onResponse(Call call, Response response) {
                    try {
                        System.out.println("===="+response.body().string());
                        System.out.println("请求成功");
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }

                @Override
                public void onFailure(Call call, Throwable t) {
                    System.out.println("请求失败");
                }
            });

            /*以上是Retrofit的请求方法*/

        }
    }

定义RetrofitPost方法接口
    public interface BlogServiceHead{
        @Multipart
        @POST("file/upload")
        Call getHead(@Query("uid") String uid, @Part MultipartBody.Part body);

    }


 
  

你可能感兴趣的:(Retrofit的POST方法上传头像)