美文网首页
Retrofit上传头像

Retrofit上传头像

作者: LJokerz | 来源:发表于2019-07-09 08:08 被阅读0次

    uploadHeadIcon

    实现了上传头像的功能

    //接口
    @POST("small/user/verify/v1/modifyHeadPic")
        Observable<Data> uploadPic(@Header("userId") String userId,
                                   @Header("sessionId") String sessionId,
                                   @Body MultipartBody part);
    
    //重要代码
    
     MultipartBody.Builder builder = new MultipartBody.Builder().setType(MultipartBody.FORM);
            File file = new File(String.valueOf(args[2]));
            builder.addFormDataPart("image", file.getName(),
                    RequestBody.create(MediaType.parse("multipart/octet-stream"),
                            file));
    
            return iRequest.uploadPic((String) args[0], (String) args[1], builder.build());
    
    //打开相册
    Intent intent = new Intent(Intent.ACTION_PICK);
            intent.setType("image/*");
            startActivityForResult(intent, PICK_CODE);
    
    //打开相机
    Intent openCameraIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
            startActivityForResult(openCameraIntent, PHOTO_CODE);
    
    //获取bitmap
    private Uri getBitmap(Bitmap bitmap, String temp) {
            File file = new File(Environment.getExternalStorageDirectory() + "/" + temp);
            if (!file.exists()) {
                file.mkdir();
            }
            File img = new File(file.getAbsolutePath() + "/temp_image.png");
            try {
                FileOutputStream fileOutputStream = new FileOutputStream(img);
                bitmap.compress(Bitmap.CompressFormat.PNG, 85, fileOutputStream);
                fileOutputStream.flush();
                fileOutputStream.close();
                return Uri.fromFile(img);
            } catch (FileNotFoundException e) {
                e.printStackTrace();
                return null;
            } catch (IOException e) {
                e.printStackTrace();
                return null;
            }
        }
    
    //Bitmap转换成file
        public File getFile(Bitmap bmp) {
            String defaultPath = getApplicationContext().getFilesDir()
                    .getAbsolutePath() + "/defaultGoodInfo";
            File file = new File(defaultPath);
            if (!file.exists()) {
                file.mkdirs();
            }
            String defaultImgPath = defaultPath + "/messageImg.jpg";
            file = new File(defaultImgPath);
            try {
                file.createNewFile();
                FileOutputStream fOut = new FileOutputStream(file);
                bmp.compress(Bitmap.CompressFormat.PNG, 20, fOut);
                fOut.flush();
                fOut.close();
            } catch (Exception e) {
                e.printStackTrace();
            }
            return file;
        }
    
    //相机返回数据
     if (data != null) {
        //获取相机图片获取
        Bitmap bitmap = data.getParcelableExtra("data");
        File file = getFile(bitmap);
        mMainPresenter.reqeust(userid, sessionid, file);
        Glide.with(this).load(bitmap).into(mIv);
    }
    
    //相册返回数据
       Uri uri = data.getData();
       if (uri != null) {
           try {
                 InputStream inputStream = getContentResolver().openInputStream(uri);
                 Bitmap bitmap = BitmapFactory.decodeStream(inputStream);
                 File file = getFile(bitmap);
                 mMainPresenter.reqeust(userid, sessionid, file);
                Glide.with(this).load(bitmap).into(mIv);
               } catch (FileNotFoundException e) {
                     e.printStackTrace();
          }
    }
    

    相关文章

      网友评论

          本文标题:Retrofit上传头像

          本文链接:https://www.haomeiwen.com/subject/zsktkctx.html