I am trying to capture Image using camera Intent and send it to server.
I have followed the official doc https://developer.android.com/training/camera/photobasics
But getting exception while using FileProvider to get the Uri from the filepath.
I am getting an exception
ERROR -
Failed to find configured root that contains /storage/emulated/0/Android/data/com.example.capture/files/Pictures/20200703_162914_2747394966410567504.jpg
filepaths.xml
<paths xmlns:android="http://schemas.android.com/apk/res/android">
<cache-path name="my_images" path="."/>
</paths>
CODE-JAVA
private File createImageFile() throws IOException {
// Create an image file name
String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());
String imageFileName = timeStamp + "_";
File storageDir = getExternalFilesDir(Environment.DIRECTORY_PICTURES);
File image = File.createTempFile(
imageFileName, /* prefix */
".jpg", /* suffix */
storageDir /* directory */
);
// Save a file: path for use with ACTION_VIEW intents
currentPhotoPath = image.getAbsolutePath();
return image;
}
private void dispatchTakePictureIntent() {
Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
// Ensure that there's a camera activity to handle the intent
if (takePictureIntent.resolveActivity(getPackageManager()) != null) {
// Create the File where the photo should go
File photoFile = null;
try {
photoFile = createImageFile();
} catch (IOException ex) {
Log.d("mylog", "Exception while creating file: " + ex.toString());
// Error occurred while creating the File
}
// Continue only if the File was successfully created
if (photoFile != null) {
Log.d("mylog", photoFile.getAbsolutePath());
Uri photoURI = FileProvider.getUriForFile(this,
"com.example.android.fileprovider",
photoFile);
takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT, photoURI);
startActivityForResult(takePictureIntent, CAMERA_REQUEST);
}
}
}
Manifest
<application>
...
...
...
<provider
android:name="androidx.core.content.FileProvider"
android:authorities="com.example.android.fileprovider"
android:grantUriPermissions="true"
android:exported="false">
<meta-data
android:name="android.support.FILE_PROVIDER_PATHS"
android:resource="@xml/filepath" />
</provider>
</application>
Your FileProvider
path should point to the location where you file resides.
Change this in filepaths.xml
:
<paths xmlns:android="http://schemas.android.com/apk/res/android">
<cache-path name="my_images" path="."/>
</paths>
To this:
<paths xmlns:android="http://schemas.android.com/apk/res/android">
<external-files-path name="Pictures" path="Android/data/com.example.capture/files/Pictures" />
</paths>