Retrofit网络请求接口上传头像

一、权限




二、拼接接口

 @Multipart
    @POST("small/user/verify/v1/modifyHeadPic")
    Observable getimage(@Header("userId")String userId, @Header("sessionId")String sessionId,@Part MultipartBody.Part part);

三、Presenter层请求数据

public class HeaderPresenter extends BasePresenter {
    public void getheader(MultipartBody.Part part){
        Apinterface api = RetrofitUtil.getInstance().create(Apinterface.class);
        api.getimage("2195", "15569761189202195", part).subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Consumer() {
                    @Override
                    public void accept(ImageBean imageBean) throws Exception {
                        getView().onSuccess(imageBean);
                    }
                });
    }
}

四、xml

1.主页面




    
    
        
        
        
        
        
        
        
        

        
    

2.Popwindow弹框



    

五、主页面

public class MineFragment extends Fragment implements View.OnClickListener,IShowView {

    @BindView(R.id.zi)
    TextView zi;
    @BindView(R.id.data_touxiang)
    TextView dataTouxiang;
    @BindView(R.id.data_image_header)
    SimpleDraweeView dataImageHeader;
    @BindView(R.id.xian1)
    View xian1;
    @BindView(R.id.data_nicheng)
    TextView dataNicheng;
    @BindView(R.id.data_text_name)
    TextView dataTextName;
    @BindView(R.id.xian2)
    View xian2;
    @BindView(R.id.data_mima)
    TextView dataMima;
    @BindView(R.id.data_text_pass)
    TextView dataTextPass;
    @BindView(R.id.xian3)
    View xian3;
    @BindView(R.id.dat_rela)
    RelativeLayout datRela;
    private View view;
    private Unbinder bind;
    private Dialog dialog;
    private File file;
    private static final int PHOTO_REQUEST_CAREMA=1;//拍照
    private static final int PHOTO_REQUEST_GALLERY=2;//从相册中选择
    private static final int PHOTO_REQUEST_CUT=3;//裁剪之后
    private static final String PHOTO_FILE_MAME="header_image.jpg";//临时文件名
    private String path=Environment.getExternalStorageDirectory()+"/header_image.png";
    private HeaderPresenter headerPresenter;

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        view = inflater.inflate(R.layout.mine, null);
        bind = ButterKnife.bind(this, view);
        initView();
        return view;
    }

    private void initView() {
        dataImageHeader.setOnClickListener(this);
        headerPresenter = new HeaderPresenter();
        headerPresenter.setView(this);
        dataImageHeader.setImageURI(Uri.parse("res://com.onenice.www/" + R.mipmap.touxiang_wdf));
    }

    @Override
    public void onClick(View v) {
        int id = v.getId();
        switch (id){
            case R.id.data_image_header:
                show();
                break;
            case R.id.takePhoto:
                //打开相机
                Intent intent_takePhoto=new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
                if (hasSdcard()){//判断SD卡是否可用
                    file=new File(Environment.getExternalStorageDirectory(),PHOTO_FILE_MAME);
                    //存放到内存中
                    intent_takePhoto.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(new File(path)));

                }
                startActivityForResult(intent_takePhoto, PHOTO_REQUEST_CAREMA);

                dialog.dismiss();
                break;
            case R.id.choosePhoto:
                Intent intent_choosePhoto=new Intent(Intent.ACTION_PICK);
                intent_choosePhoto.setType("image/*");
                startActivityForResult(intent_choosePhoto,PHOTO_REQUEST_GALLERY);

                dialog.dismiss();
                break;
            case R.id.btn_cancel:
                dialog.dismiss();
                break;
            case R.id.cancle_login:
                break;
        }
    }

    private void show() {
        dialog = new Dialog(getActivity(), R.style.ActionSheetDialogStyle);
        //填充对话框的布局
        View inflate = LayoutInflater.from(getActivity()).inflate(R.layout.dialog_layout, null);
        //初始化控件
        inflate.findViewById(R.id.choosePhoto).setOnClickListener(this);
        inflate.findViewById(R.id.takePhoto).setOnClickListener(this);
        inflate.findViewById(R.id.btn_cancel).setOnClickListener(this);
        inflate.findViewById(R.id.cancle_login).setOnClickListener(this);
        //将布局设置给Dialog
        dialog.setContentView(inflate);
        //获取当前Activity所在的窗体
        Window dialogWindow = dialog.getWindow();
        if(dialogWindow == null){
            return;
        }
        //设置Dialog从窗体底部弹出
        dialogWindow.setGravity(Gravity.BOTTOM);
        //获得窗体的属性
        WindowManager.LayoutParams lp = dialogWindow.getAttributes();
        lp.y = 20;//设置Dialog距离底部的距离
        //将属性设置给窗体
        dialogWindow.setAttributes(lp);
        dialog.show();//显示对话框
    }

    //裁剪图片
    private void crop(Uri uri){
        Intent intent=new Intent("com.android.camera.action.CROP");
        intent.setDataAndType(uri,"image/*");
        //支持裁剪
        intent.putExtra("CROP",true);
        //裁剪的比例
        intent.putExtra("aspectX",1);
        intent.putExtra("aspectY",1);
        //裁剪后输出图片的尺寸大小
        intent.putExtra("outputX",250);
        intent.putExtra("outputY",250);
        //将图片返回给data
        intent.putExtra("return-data",true);
        startActivityForResult(intent,PHOTO_REQUEST_CUT);
    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (requestCode==PHOTO_REQUEST_CAREMA&&resultCode==getActivity().RESULT_OK){//相机返回的数据
            if (hasSdcard()){
                crop(Uri.fromFile(new File(path)));
            }else{
                Toast.makeText(getActivity(), "未找到存储啦,无法存储照片", Toast.LENGTH_SHORT).show();
            }
        }else if (requestCode==PHOTO_REQUEST_GALLERY&&resultCode==getActivity().RESULT_OK){//相册返回的数据
            //得到图片的全路径
            if (data!=null){
                Uri uri = data.getData();
                crop(uri);
            }
        }else if(requestCode==PHOTO_REQUEST_CUT&&resultCode==getActivity().RESULT_OK){//剪切回来的照片数据
            if (data!=null){
                Bitmap bitmap = data.getParcelableExtra("data");
                //将bitmap转换为uri
                Uri uri = Uri.parse(MediaStore.Images.Media.insertImage(getActivity().getContentResolver(), bitmap, null,null));

                String[] proj = { MediaStore.Images.Media.DATA };

                Cursor actualimagecursor = getActivity().managedQuery(uri,proj,null,null,null);

                int actual_image_column_index = actualimagecursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);

                actualimagecursor.moveToFirst();

                String img_path = actualimagecursor.getString(actual_image_column_index);

                File file = new File(img_path);

                //判断file文件是否存在
                if (file != null && file.exists()){
                    RequestBody requestBody = RequestBody.create(MediaType.parse("image/*"),file.getAbsoluteFile());
                    MultipartBody.Part part = MultipartBody.Part.createFormData("image",file.getName(),requestBody);

                    headerPresenter.getheader(part);
                }else {
                    Toast.makeText(getActivity(),"请选择文件",Toast.LENGTH_SHORT).show();
                }

            }

        }

        super.onActivityResult(requestCode, resultCode, data);
    }

    @Override
    public void onSuccess(ImageBean imageBean) {
        Toast.makeText(getActivity(),imageBean.getMessage(),Toast.LENGTH_SHORT).show();
        String headPath = imageBean.getHeadPath();
        dataImageHeader.setImageURI(headPath);
    }

    @Override
    public void onFailed(String err) {

    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        bind.unbind();
    }
    //判断SD卡是否挂载
    public boolean hasSdcard(){
        if (Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)){
            return true;
        }else{
            return false;
        }
    }


}

六、values

Retrofit网络请求接口上传头像_第1张图片

1.dialog_res.xml



    
        
        #ffffff
        #d9d9d9
        #131313
        
        17sp
        
        拍照
        从相册选择
        取消
        
        
        
        
        
    

2.anim_dialog_top_in


3.anim_dialog_top_out 


 

你可能感兴趣的:(基础)