I have a camera intent that takes a photo then returns back to the activity. It works fine on the emulator but on a real device, it crashes after taking the photo and accepting it.
The error I get on the device is this
Process: com.example.smartcalendar, PID: 29675
java.lang.RuntimeException: Failure delivering result ResultInfo{who=null, request=101, result=-1, data=null} to activity {com.example.smartcalendar/com.example.smartcalendar.DailyActivity}: java.lang.NullPointerException: null reference
at android.app.ActivityThread.deliverResults(ActivityThread.java:4605)
at android.app.ActivityThread.handleSendResult(ActivityThread.java:4647)
at android.app.servertransaction.ActivityResultItem.execute(ActivityResultItem.java:49)
at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:108)
at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:68)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1948)
at android.os.Handler.dispatchMessage(Handler.java:106)
at android.os.Looper.loop(Looper.java:214)
at android.app.ActivityThread.main(ActivityThread.java:7045)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:964)
Caused by: java.lang.NullPointerException: null reference
at com.google.android.gms.common.internal.Preconditions.checkNotNull(com.google.android.gms:play-services-basement@@17.1.1:2)
at com.google.mlkit.vision.common.InputImage.<init>(com.google.mlkit:vision-common@@16.1.0:74)
at com.google.mlkit.vision.common.InputImage.fromBitmap(com.google.mlkit:vision-common@@16.1.0:12)
at com.example.smartcalendar.DailyActivity.onActivityResult(DailyActivity.java:236)
at android.app.Activity.dispatchActivityResult(Activity.java:7759)
at android.app.ActivityThread.deliverResults(ActivityThread.java:4598)
at android.app.ActivityThread.handleSendResult(ActivityThread.java:4647)
at android.app.servertransaction.ActivityResultItem.execute(ActivityResultItem.java:49)
at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:108)
at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:68)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1948)
at android.os.Handler.dispatchMessage(Handler.java:106)
at android.os.Looper.loop(Looper.java:214)
at android.app.ActivityThread.main(ActivityThread.java:7045)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:964)
These are the relevant code blocks
Intent camintent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
// Ensure that there's a camera activity to handle the intent
if (camintent.resolveActivity(getPackageManager()) != null) {
// Create the File where the photo should go
File photoFile;
photoFile = null;
try {
photoFile = **createImageFile();**
} catch (IOException ex) {
// Error occurred while creating the File
Log.e(TAG, "Error occured creating the file");
}
// Continue only if the File was successfully created
if (photoFile != null) {
Uri photoURI = FileProvider.getUriForFile(this,
"com.example.android.fileprovider",
photoFile);
camintent.putExtra(MediaStore.EXTRA_OUTPUT, photoURI);
**startActivityForResult(camintent, 101);**
}
}
That launches the activityonresult but before that, it calls this method to createImageFile.
String currentPhotoPath;
private File **createImageFile()** throws IOException {
// String currentPhotoPath;
// Create an image file name
String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());
String imageFileName = "JPEG_" + 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;
}
Inside the onActivityResult, this is the code I have for now.
// Bitmap takenImage = BitmapFactory.decodeFile(photoFile.getAbsolutePath());
Bitmap bm=null;
if (data != null) {
bm = BitmapFactory.decodeFile(currentPhotoPath);
ByteArrayOutputStream out = new ByteArrayOutputStream();
Log.e(TAG, "onActivityResult: "+ BitmapCompat.getAllocationByteCount(bm));
bm.compress(JPEG, 100, out);
Bitmap decoded = BitmapFactory.decodeStream(new ByteArrayInputStream(out.toByteArray()));
Log.e(TAG, "onActivityResult: "+ BitmapCompat.getAllocationByteCount(decoded));
}
Here is the provider in the Android Manifest
<provider
android:name="androidx.core.content.FileProvider"
android:authorities="com.example.android.fileprovider"
android:exported="false"
android:grantUriPermissions="true">
<meta-data
android:name="android.support.FILE_PROVIDER_PATHS"
android:resource="@xml/file_paths"></meta-data>
</provider>
Here is file_paths.xml
<paths xmlns:android="http://schemas.android.com/apk/res/android">
<external-path name="my_images" path="." />
</paths>
For the file_paths.xml I tried to follow this answers' advice.
My suspicions are that the problem lies somewhere in trying to save the photo to storage and retrieve it later as well.