为捕获的图像创建单独的文件夹时FileUriExposedException

时间:2018-04-11 09:17:01

标签: android android-camera

我正在尝试使用以下代码将相机捕获的图像存储在单独的文件夹中,但是当我执行此代码时,我得到了异常,我尝试了很多解决方案,但没有用,可以帮助我一些

  

android.os.FileUriExposedException:file:///storage/emulated/0/MyRamImages/FILENAME.jpg通过ClipData.Item.getUri()

暴露在app之外

代码: -

File imagesFolder = new File(Environment.getExternalStorageDirectory(), "MyImages");
imagesFolder.mkdirs();
Random generator = new Random();
int n = 10000;
n = generator.nextInt(n);
String fname = "FILENAME-"+ n +".jpg";
File image = new File(imagesFolder, fname);
Uri uriSavedImage = Uri.fromFile(image);
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
intent.putExtra(MediaStore.EXTRA_OUTPUT, uriSavedImage);
startActivityForResult(intent, 100);

2 个答案:

答案 0 :(得分:1)

首先捕获图像,然后在onActivityResult中将图像作为位图,然后将其保存到要保存的路径。

private void openCamera()
{
        // Start the camera and take the image
        // handle the storage part in on activity result
        Intent cameraIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
        startActivityForResult(cameraIntent, CAPTURE_IMAGE_REQUEST_CODE);
}

在活动结果方法里面写下面的代码。

if (requestCode == CAPTURE_IMAGE_REQUEST_CODE)
{
     if (resultCode == RESULT_OK)
     {
         Bitmap imgBitmap = (Bitmap) data.getExtras().get("data");
         File sd = Environment.getExternalStorageDirectory();
         File imageFolder = new File(sd.getAbsolutePath() + File.separator +
                        "FolderName" + File.separator + "InsideFolderName");

         if (!imageFolder.isDirectory())
         {
              imageFolder.mkdirs();
         }

         File mediaFile = new File(imageFolder + File.separator + "img_" +
                            System.currentTimeMillis() + ".jpg");

         FileOutputStream fileOutputStream = new FileOutputStream(mediaFile);
         imgBitmap.compress(Bitmap.CompressFormat.JPEG, 90, fileOutputStream);
         fileOutputStream.close();
     }
}

这也适用于7.1.1及更低版本。

答案 1 :(得分:0)

而不是返回Uri.fromFile(mediaFile);

return FileProvider.getUriForFile(MainActivity.this,
                                  BuildConfig.APPLICATION_ID + ".provider",
                                  mediaFile);

这需要您向AndroidManifest添加提供程序:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
  ...
  <application
  ...
  <provider
    android:name="android.support.v4.content.FileProvider"
    android:authorities="${applicationId}.provider"
    android:exported="false"
    android:grantUriPermissions="true">
    <meta-data
        android:name="android.support.FILE_PROVIDER_PATHS"
        android:resource="@xml/provider_paths"/>
  </provider>
 </application>

然后在res文件夹下的xml文件夹中创建一个provider_paths.xml文件。

<?xml version="1.0" encoding="utf-8"?>
<paths xmlns:android="http://schemas.android.com/apk/res/android">
<external-path name="external_files" path="."/>
</paths>

参考此https://inthecheesefactory.com/blog/how-to-share-access-to-file-with-fileprovider-on-android-nougat/en