public void takePic(){ String forderPath = getExternalFilesDir("") + "/pic"; File file = new File(forderPath); if (!file.exists()){ file.mkdirs(); } String randomNum = DateTools.getCurrentData_yyyyMMddHHmmss(); //创建File对象,用于存储拍照后的照片 File outputImage = new File(forderPath,randomNum + "_output_image.jpg"); currentImagPath = outputImage.getAbsolutePath(); try { if (outputImage.exists()){ outputImage.delete(); } outputImage.createNewFile(); }catch (IOException e){ e.printStackTrace(); } if (Build.VERSION.SDK_INT >= 24){ imageUri = FileProvider.getUriForFile(context,"xxxxxx.packagename",outputImage); }else { imageUri = Uri.fromFile(outputImage); } //启动相机程序 Intent intent = new Intent("android.media.action.IMAGE_CAPTURE"); intent.putExtra(MediaStore.EXTRA_OUTPUT,imageUri); startActivityForResult(intent,TAKE_PHOTO); }
以上是拍照程序
下面是处理
@Override protected void onActivityResult(int requestCode, int resultCode, Intent data) { switch (requestCode){ case TAKE_PHOTO: if (resultCode == RESULT_OK){ // String mimgUri = imageUri.toString(); // String path = imageUri.getPath(); String path = getProviderUriPath(imageUri); Uri uri = Uri.parse(path); mUploadMessage.onReceiveValue(uri); } break; } }
其中getProviderUriPath工具类可以查看我上一篇文章