In fragment I want to take a photo but I have a problem , I never get a callback to onActivityResult
My code :
private void dispatchTakePictureIntent() {
int width = 960;
Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.M) {
Intent cameraIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
if (cameraIntent.resolveActivity(getActivity().getPackageManager()) != null) {
// Create the File where the photo should go
File photoFile = null;
try {
photoFile = createImageFile();
} catch (IOException ex) {
Log.e("error",ex.getMessage());
}
// Continue only if the File was successfully created
if (photoFile != null) {
Uri photoURI = null;
try {
photoURI = FileProvider.getUriForFile(getActivity(), BuildConfig.APPLICATION_ID + ".provider", createImageFile());
} catch (IOException e) {
e.printStackTrace();
}
// cameraIntent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(photoFile));
cameraIntent.putExtra(MediaStore.EXTRA_OUTPUT, photoURI);
startActivityForResult(cameraIntent, REQUEST_IMAGE_CAPTURE);
}
}
} else if (takePictureIntent.resolveActivity(getActivity().getPackageManager()) != null) {
File photoFile = null;
try {
photoFile = createImageFile();
} catch (IOException ignored) {
}
if (photoFile != null) {
mCurrentPhotoPath = photoFile;
takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(mCurrentPhotoPath));
startActivityForResult(takePictureIntent, REQUEST_TAKE_PHOTO);
}
}
}
private File createImageFile() throws IOException {
String timeStamp = new SimpleDateFormat("yyyyMMddHHmmss").format(new Date());
String imageFileName = "smok" + timeStamp;
File image = File.createTempFile(
imageFileName, /* prefix */
".jpg", /* suffix */
imagesDir /* directory */
);
mCurrentPhotoPath = new File(image.getAbsolutePath());
return image;
}
onActivityResult
never gets called. I kept break point on the first line of onActivityResult
method but it does not get called and I don't know why
you are using Uri.fromFile(mCurrentPhotoPath)
but you should use FileProvider instead use below code
FileProvider.getUriForFile(mContext, mContext.getPackageName() + ".provider", photoFile)
EDIT
Don't forget to register for FileProvider in the AndroidManifest.xml
<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>
inside res package create a subfolder called xml and provider_paths.xml
provider_paths.xml
content
<?xml version="1.0" encoding="utf-8"?>
<paths xmlns:android="http://schemas.android.com/apk/res/android">
<!--<external-path name="external_files" path="."/>-->
<external-path name="external_files" path="/"/>
</paths>
EDIT 2
You're passing newFile object inside FileProvider method. instead, use the one you already created. Also, you're using getActivity()
as context you should fragment context instead. see below:
try {
photoURI = FileProvider.getUriForFile(getContext(), getContext().getPackageName() + ".provider", photoFile);
} catch (IOException e) {
e.printStackTrace();
}
EDIT 3
Call super.onActivityResult()
inside activity