I am quite new in android programming and writing shop assistant with scan from camera. I use fileprovider to not loose quality of photos using standard camera app. But on some phones app crash on moment of taking picture when I press tick in camera app:
In this moment i press tick and app crashes on Some phones (but on some works)
Here is my code of provider:
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/provider_paths" />
</provider>
provider_paths:
<?xml version="1.0" encoding="utf-8"?>
<paths>
<external-path name="external_files" path="/"/>
</paths>
taking picture:
private void takePicture() {
try{
Intent cameraIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
try {
String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());
String imageFileName = timeStamp + ".jpg";
File storageDir = Environment.getExternalStoragePublicDirectory(
Environment.DIRECTORY_PICTURES);
pictureImagePath = storageDir.getAbsolutePath() + "/" + imageFileName;
File file = new File(pictureImagePath);
Uri outputFileUri = FileProvider.getUriForFile(this, this.getApplicationContext().getPackageName() + ".provider", file);
cameraIntent.putExtra(MediaStore.EXTRA_OUTPUT, outputFileUri);
} catch (Exception ignored) {
} finally {
someActivityResultLauncher.launch(cameraIntent);
}
}catch (ActivityNotFoundException e){
Toast.makeText(this, getString(R.string.camera_not_recog), Toast.LENGTH_SHORT).show();
}
}
ActivityResultLauncher<Intent> someActivityResultLauncher = registerForActivityResult(
new ActivityResultContracts.StartActivityForResult(),
new ActivityResultCallback<ActivityResult>() {
/**
* Method to take user's photo
* @param result
*/
@Override
public void onActivityResult(ActivityResult result) {
if (result.getResultCode() == Activity.RESULT_OK) {
Intent data = result.getData();
try {
File imgFile = new File(pictureImagePath);
// Check if file exists
if (imgFile.exists()) {
bitmap = BitmapFactory.decodeFile(imgFile.getAbsolutePath());
} else {
assert data != null;
Bundle extras = data.getExtras();
bitmap = (Bitmap) extras.get("data");
}
} catch (Exception e) {
assert data != null;
Bundle extras = data.getExtras();
bitmap = (Bitmap) extras.get("data");
} finally {
Intent intent = new Intent(getApplicationContext(), CameraActivity.class);
startActivity(intent);
}
}
}
});