Fileuriexposedexception When Create Separate Folder For Captured Images
I am trying to store camera captured images at separate folder using below code but when i execute this code i am getting exception and i tried lot for getting solution but no use
Solution 1:
First just capture the image then in onActivityResult get the image as bitmap then save that to the path you want to save.
privatevoidopenCamera()
{
        // 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);
}
And inside on activity result method write the following code.
if (requestCode == CAPTURE_IMAGE_REQUEST_CODE)
{
     if (resultCode == RESULT_OK)
     {
         BitmapimgBitmap= (Bitmap) data.getExtras().get("data");
         Filesd= Environment.getExternalStorageDirectory();
         FileimageFolder=newFile(sd.getAbsolutePath() + File.separator +
                        "FolderName" + File.separator + "InsideFolderName");
         if (!imageFolder.isDirectory())
         {
              imageFolder.mkdirs();
         }
         FilemediaFile=newFile(imageFolder + File.separator + "img_" +
                            System.currentTimeMillis() + ".jpg");
         FileOutputStreamfileOutputStream=newFileOutputStream(mediaFile);
         imgBitmap.compress(Bitmap.CompressFormat.JPEG, 90, fileOutputStream);
         fileOutputStream.close();
     }
}
This works for me in 7.1.1 and lower versions as well.
Solution 2:
Instead of return Uri.fromFile(mediaFile); do
return FileProvider.getUriForFile(MainActivity.this,
                                  BuildConfig.APPLICATION_ID + ".provider",
                                  mediaFile);
That would require you to add a provider to the AndroidManifest:
<?xml version="1.0" encoding="utf-8"?><manifestxmlns:android="http://schemas.android.com/apk/res/android"...
  <application...
  <providerandroid:name="android.support.v4.content.FileProvider"android:authorities="${applicationId}.provider"android:exported="false"android:grantUriPermissions="true"><meta-dataandroid:name="android.support.FILE_PROVIDER_PATHS"android:resource="@xml/provider_paths"/></provider></application>And then create a provider_paths.xml file in xml folder under res folder.
<?xml version="1.0" encoding="utf-8"?><pathsxmlns:android="http://schemas.android.com/apk/res/android"><external-pathname="external_files"path="."/></paths>Once refer this https://inthecheesefactory.com/blog/how-to-share-access-to-file-with-fileprovider-on-android-nougat/en
Post a Comment for "Fileuriexposedexception When Create Separate Folder For Captured Images"