2021年4月30日星期五

getExternalFilesDir not corresponding to FileProvider path

I'm trying to get a full size photo from a device with api >= 24 and I'm stuck with file provider.

I've done a lot of research on the official docs, SO, etc but I'm not able to get this to work.

After a lot of tries I endend setting up the same code that the official docs have and still not working.

My Manifest:

<provider          android:name="androidx.core.content.FileProvider"          android:authorities="${applicationId}.provider"          android:exported="false"          android:grantUriPermissions="true">          <meta-data              android:name="android.support.FILE_PROVIDER_PATHS"              android:resource="@xml/file_paths" />      </provider>  

My file_paths.xml

<?xml version="1.0" encoding="utf-8"?>  <paths>      <external-files-path name="my_images" path="Pictures" />  </paths>  

The method I've made to create the temp file:

    public static File createNewImageFile(@NotNull Context context) throws IOException {          // Create an image file name          String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());          String imageFileName = "JPEG_" + timeStamp + "_";          File storageDir = context.getExternalFilesDir(Environment.DIRECTORY_PICTURES);          Log.d(TAG, "Storage dir: " + storageDir.getPath());          if (!storageDir.exists()){              storageDir.mkdir();          }          return File.createTempFile(imageFileName, ".jpg", storageDir);      }  

And finally, how I get the URI and call the camera Intent:

    File photoFile = null;      try {          photoFile = Common.createNewImageFile(NewRequestActivity.this);      } catch (IOException e) {          Log.e(TAG, e.getMessage(), e);      }        // Continue only if the File was successfully created      if (photoFile == null) {          // Shows a Snackbar          return;      }          if (Build.VERSION.SDK_INT >= 24) {          Log.d(TAG, "SDK Version >= 24");          currentPhotoUri = FileProvider.getUriForFile(this,                  BuildConfig.APPLICATION_ID + ".provider",                  photoFile);            Log.d(TAG, "Path currentPhoto: " + currentPhotoUri.getPath());          Log.d(TAG, "Uri currentPhoto: " + currentPhotoUri);        } else {          currentPhotoUri = Uri.fromFile(photoFile);      }        if (currentPhotoUri != null) {          takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT, currentPhotoUri);          takePictureIntent.setFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION | Intent.FLAG_GRANT_WRITE_URI_PERMISSION);          startActivityForResult(takePictureIntent, REQUEST_IMAGE_CAPTURE);      }  

When I execute this code, I can see the temp file created on the device file explorer under the following path:

/sdcard/Android/data/com.my.app/files/Pictures/JPEG_date.jpg  

And I get this output on the Logcat:

D/Logger Common: Storage dir: /storage/emulated/0/Android/data/com.my.app/files/Pictures  D/Logger NewRequestAc: SDK Version >= 24  D/Logger NewRequestAc: Path currentPhoto: /my_images/JPEG_20210501_025928_8990130582622027000.jpg  D/Logger NewRequestAc: Uri currentPhoto: content://com.my.app.provider/my_images/JPEG_20210501_025928_8990130582622027000.jpg  

And finally, when I take the photo I get the following errors and the uri sent on EXTRA_OUTPUT becomes null:

E/Perf: Fail to get file list com.brugui.dermalcheck  E/Perf: getFolderSize() : Exception_1 = java.lang.NullPointerException: Attempt to get length of null array  

I don't know why this doesn't work if I'm following the exact steps done in the official docs and it specifically says that the path given by getExternalFilesDir corresponds to the one in the external-files-path.

The path component corresponds to the path that is returned by getExternalFilesDir() when called with Environment.DIRECTORY_PICTURES

https://stackoverflow.com/questions/67342049/getexternalfilesdir-not-corresponding-to-fileprovider-path May 01, 2021 at 09:05AM

没有评论:

发表评论