<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);