How to get uri of captured photo?
Follow below steps.
Step - 1 : Create provider_paths.xml in res/xml folder and write below code in it.
<?xml version="1.0" encoding="utf-8"?>
<paths xmlns:android="http://schemas.android.com/apk/res/android">
<external-path
name="external_files"
path="." />
</paths>
Step - 2 : Declare provider in manifest as below.
<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>
If you are facing error at: android:name="android.support.v4.content.FileProvider" Use:
<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/provider_paths" />
</provider>
Step - 3 : Write your camera intent as below.
Intent m_intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
File file = new File(Environment.getExternalStorageDirectory(), "MyPhoto.jpg");
Uri uri = FileProvider.getUriForFile(this, this.getApplicationContext().getPackageName() + ".provider", file);
m_intent.putExtra(android.provider.MediaStore.EXTRA_OUTPUT, uri);
startActivityForResult(m_intent, REQUEST_CAMERA_IMAGE);
Step - 4 : Handle camera result in onActivityResult as below.
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
switch (requestCode) {
//TODO... onCamera Picker Result
case REQUEST_CAMERA_IMAGE:
if (resultCode == RESULT_OK) {
//File object of camera image
File file = new File(Environment.getExternalStorageDirectory(), "MyPhoto.jpg");
//Uri of camera image
Uri uri = FileProvider.getUriForFile(this, this.getApplicationContext().getPackageName() + ".provider", file);
}
break;
}
}
While calling Camera intent
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
File mediaStorageDir = new File(Environment.getExternalStoragePublicDirectory(
Environment.DIRECTORY_PICTURES), "IMG_FOLDER");
try {
if (!mediaStorageDir.exists()) {
if (!mediaStorageDir.mkdirs()) {
return null;
}
}
} catch (Exception e) {
e.printStackTrace();
}
imageURI = Uri.fromFile(new File(mediaStorageDir.getPath() + File.separator +
"profile_img.jpg"));
intent.putExtra(MediaStore.EXTRA_OUTPUT, imageURI);
startActivityForResult(intent, Utils.CAMERA_REQUEST);
Inside onActivityResult
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
try {
if (resultCode == RESULT_OK) {
if (requestCode == Utils.CAMERA_REQUEST) {
if (imageURI != null) { // use the same uri, that you initialized while calling camera intent
// do whatever you want to do with this Uri
}
}
}
} catch(Exception E) {}
}
For Nougat, you need to add this below code in splash / launcher activity
if (Build.VERSION.SDK_INT >= 24) {
try {
Method m = StrictMode.class.getMethod("disableDeathOnFileUriExposure");
m.invoke(null);
} catch (Exception e) {
e.printStackTrace();
}
}
This will work in all version. I have tested it too.