多图上传

<provider
    android:name="androidx.core.content.FileProvider"
    android:authorities="iec.hydrology.polling.fileprovider"
    android:exported="false"
    android:grantUriPermissions="true">
    <meta-data
        android:name="android.support.FILE_PROVIDER_PATHS"
        android:resource="@xml/file_paths"
        tools:replace="android:resource" />
</provider>
<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android">
    <root-path path="" name="my_images" />
</PreferenceScreen>

 

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    if (requestCode == REQUEST_CAMERA && resultCode == RESULT_OK) {
        FileProvider.getUriForFile(this, "iec.hydrology.polling.fileprovider", file);
        //在手机相册中显示刚拍摄的图片
        showImage(file);
        Intent mediaScanIntent = new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE);
        Uri contentUri = Uri.fromFile(file);
        isNull=false;
        mediaScanIntent.setData(contentUri);
        sendBroadcast(mediaScanIntent);
        try {
            Bitmap bitmap = MediaStore.Images.Media.getBitmap(this.getContentResolver(), contentUri);
            imglist.remove(position);
            imglist.add(new ChooseImgModel(0,bitmap,null));
            imglist.add(new ChooseImgModel(0,bitmaps,null));//默认最后一张
            imgAdaptel.setList(imglist);
            imgAdaptel.notifyDataSetChanged();
        } catch (IOException e) {
            e.printStackTrace();
        }


    }
    if (requestCode == IMAGE_REQUEST_CODE && resultCode == RESULT_OK) {
        try {
            Uri selectedImage = data.getData(); //获取系统返回的照片的Uri
            String[] filePathColumn = {MediaStore.Images.Media.DATA};
            Cursor cursor = getContentResolver().query(selectedImage,
                    filePathColumn, null, null, null);//从系统表中查询指定Uri对应的照片
            cursor.moveToFirst();
            int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
            paths = cursor.getString(columnIndex); //获取照片路径
            cursor.close();
            Bitmap bitmap = BitmapFactory.decodeFile(paths);

        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

 

 

 

public void applyWritePermission() {
    String[] permissions = {Manifest.permission.WRITE_EXTERNAL_STORAGE};
    if (Build.VERSION.SDK_INT >= 23) {
        int check = ContextCompat.checkSelfPermission(this, permissions[0]);
        // 权限是否已经 授权 GRANTED---授权 DINIED---拒绝
        if (check == PackageManager.PERMISSION_GRANTED) {
            useCamera();
        } else {
            requestPermissions(new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE}, 1);
        }
    } else {
        useCamera();
    }
}

 

 

private void useCamera() {
    Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
    file = new File(Environment.getExternalStorageDirectory().getAbsolutePath()
            + "/test/" + System.currentTimeMillis() + ".jpg");
    file.getParentFile().mkdirs();
    //改变Uri iec.hydrology.polling.fileprovider注意和xml中的一致
    Uri uri = FileProvider.getUriForFile(this, "iec.hydrology.polling.fileprovider", file);
    //添加权限
    intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
    intent.putExtra(MediaStore.EXTRA_OUTPUT, uri);
    startActivityForResult(intent, REQUEST_CAMERA);
}

 

 public void uploadImgs(List<File> pathList,int id,String code){

    Map<String, RequestBody> map = new HashMap<>();
    map.put("taskId", RxPartMapUtils.toRequestBodyOfText(id+""));
    map.put("stationCode", RxPartMapUtils.toRequestBodyOfText(code));

      for (int i = 0; i <pathList.size() ; i++) {
          File file = pathList.get(i);

          if(file.exists()) {
              RequestBody _requestBody =   RxPartMapUtils.toRequestBodyOfImage(file);
              map.put("file\"; filename=\""+file.getName()+"", _requestBody);
          }else {
              Log.d("uploadImgs",""+pathList.get(0).getPath());
              return;
          }

      }

    publicApi.uploadMore(map)
            .map(new Func1<ResponseModel, ResponseModel>() {
                @Override
                public ResponseModel call(ResponseModel responseModel) {
                    return responseModel;
                }
            }).subscribeOn(Schedulers.io())
            .observeOn(AndroidSchedulers.mainThread())
            .subscribe(new Subscriber<ResponseModel>() {
                @Override
                public void onCompleted() {

                }

                @Override
                public void onError(Throwable e) {

                    Log.d("onError",""+e);

                }

                @Override
                public void onNext(ResponseModel responseModel) {
                    int code = responseModel.getCode();
                    if (code==200){
                        ViewKit.showToast(context, "图片上传成功");
                    }else {
                        ViewKit.showToast(context, "图片上传失败");
                    }

                }
            });


}

 

 

//上传图
@Multipart
@POST("app/task/uploadPhoto")
Observable<ResponseModel> uploadMore(@PartMap Map<String, RequestBody> map);

 

 

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

梦想不上班

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值