I have this app that was working perfectly until the business using the app bought everyone Huawei Y5lite running android go, then the app started crushing while taking images. I have been trying to crack this the last four days but without success. The crashes are random and do not follow any pattern, meaning the app can take several images without crashing then at some point crash which makes me think my activity is being killed when in background and camera on the foreground and so onActivityResult fails. i have tried to save the image URL onSaveInstanceState and retrieve it onCreate but doesn't solve it below is my relevant code and errors
private void launchCamera() {
Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
if (takePictureIntent.resolveActivity(getPackageManager()) != null) {
File photoFile = null;
try {
photoFile = BitmapUtils.createTempImageFile(this);
} catch (IOException ex) {
ex.printStackTrace();
}
if (photoFile != null) {
mTempPhotoPath = photoFile.getAbsolutePath();
// Get the content URI for the image file
Uri photoURI = FileProvider.getUriForFile(this,
FILE_PROVIDER_AUTHORITY,
photoFile);
// Add the URI so the camera can store the image
takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT, photoURI);
// Launch the camera activity
startActivityForResult(takePictureIntent, REQUEST_IMAGE_CAPTURE);
}
}
}
//saving the filename incase activity is killed
@Override
public void onSaveInstanceState(Bundle bundle)
{
super.onSaveInstanceState(bundle);
bundle.putString("fileName", mTempPhotoPath);
}
//retrieving file name onCreate
if (savedInstanceState != null){
mTempPhotoPath = savedInstanceState.getString("fileName");}
// processing the image
private void processAndSetImage() {
switch (imageTaken) {
case "imageOne":
//I did the try-catch to see if I can isolate the issue but still crashes
try{
mAddImageOne.setVisibility(View.GONE);
mResultsBitmap = BitmapUtils.resamplePic(this, mTempPhotoPath);
mOneView.setVisibility(View.VISIBLE);
mOneView.setImageBitmap(mResultsBitmap);
findViewById(R.id.front_image_text).setVisibility(View.VISIBLE);
findViewById(R.id.save_cancel).setVisibility(View.VISIBLE);
}catch (Exception e){
Toast toast = Toast.makeText(getApplicationContext(), "Please try take the picture again",
Toast.LENGTH_LONG);
toast.setGravity(Gravity.BOTTOM, 0, 1100);
toast.show();
}
break;
case "imageTwo":
try{
mAddImageTwo.setVisibility(View.GONE);
mTwoView.setImageBitmap(mResultsBitmap);
mResultsBitmap = BitmapUtils.resamplePic(this, mTempPhotoPath);
findViewById(R.id.front_image_label_text).setVisibility(View.VISIBLE);
findViewById(R.id.save_cancel).setVisibility(View.VISIBLE);
mTwoView.setVisibility(View.VISIBLE);
}catch (Exception e){
Toast toast = Toast.makeText(getApplicationContext(), "Please try take the picture again",
Toast.LENGTH_LONG);
toast.setGravity(Gravity.BOTTOM, 0, 1100);
toast.show();
}
break;
}
Below is the error I am getting when the crash occurs. (Sometimes the photo taking and processing occurs well, sometime the crash happens)
02-05 13:53:22.139 23081-23086/com.avigail.tuborg I/zygote: Do partial code cache collection, code=25KB, data=30KB 02-05 13:53:22.140 23081-23086/com.avigail.tuborg I/zygote: After code cache collection, code=25KB, data=30KB 02-05 13:53:22.141 23081-23086/com.avigail.tuborg I/zygote: Increasing code cache capacity to 128KB 02-05 13:53:22.277 23081-23081/com.avigail.tuborg D/AndroidRuntime: Shutting down VM 02-05 13:53:22.284 23081-23081/com.avigail.tuborg E/AndroidRuntime: FATAL EXCEPTION: main Process: com.avigail.tuborg, PID: 23081 java.lang.RuntimeException: Unable to resume activity {com.avigail.tuborg/com.avigail.kaskazi.activities.StockistActivity}: java.lang.RuntimeException: Failure delivering result ResultInfo{who=null, request=1, result=-1, data=null} to activity {com.avigail.tuborg/com.avigail.kaskazi.activities.StockistActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'int java.lang.String.hashCode()' on a null object reference at android.app.ActivityThread.performResumeActivity(ActivityThread.java:3844) at android.app.ActivityThread.handleResumeActivity(ActivityThread.java:3884) at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3053) at android.app.ActivityThread.-wrap11(Unknown Source:0) at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1777) at android.os.Handler.dispatchMessage(Handler.java:106) at android.os.Looper.loop(Looper.java:166) at android.app.ActivityThread.main(ActivityThread.java:6861) at java.lang.reflect.Method.invoke(Native Method) at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:450) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:936) Caused by: java.lang.RuntimeException: Failure delivering result ResultInfo{who=null, request=1, result=-1, data=null} to activity {com.avigail.tuborg/com.avigail.kaskazi.activities.StockistActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'int java.lang.String.hashCode()' on a null object reference at android.app.ActivityThread.deliverResults(ActivityThread.java:4564) at android.app.ActivityThread.performResumeActivity(ActivityThread.java:3816) at android.app.ActivityThread.handleResumeActivity(ActivityThread.java:3884) at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3053) at android.app.ActivityThread.-wrap11(Unknown Source:0) at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1777) at android.os.Handler.dispatchMessage(Handler.java:106) at android.os.Looper.loop(Looper.java:166) at android.app.ActivityThread.main(ActivityThread.java:6861) at java.lang.reflect.Method.invoke(Native Method) at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:450) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:936) Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'int java.lang.String.hashCode()' on a null object reference at
com.avigail.kaskazi.activities.StockistActivity.processAndSetImage(StockistActivity.java:392)
atcom.avigail.kaskazi.activities.StockistActivity.onActivityResult(StockistActivity.java:377)
at android.app.Activity.dispatchActivityResult(Activity.java:7393) at android.app.ActivityThread.deliverResults(ActivityThread.java:4560) at android.app.ActivityThread.performResumeActivity(ActivityThread.java:3816) at android.app.ActivityThread.handleResumeActivity(ActivityThread.java:3884) at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3053) at android.app.ActivityThread.-wrap11(Unknown Source:0) at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1777) at android.os.Handler.dispatchMessage(Handler.java:106) at android.os.Looper.loop(Looper.java:166) at android.app.ActivityThread.main(ActivityThread.java:6861) at java.lang.reflect.Method.invoke(Native Method) at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:450) atcom.android.internal.os.ZygoteInit.main(ZygoteInit.java:936)
I will highly appreciate your help PS: I have see the various questions addressing the issue but none of the answers worked including checking if data is null(my data is null because am putting the extra option), saving the image URL onSaveInstanceState, znc checking if the intentfor result returns ok
This turned out to be a rather silly mistake that cost me several days. The problem as i had anticipated was my activity being killed while in the background, and the solution was properly saving the state.
I was saving the imageUrl, alright, but was missing on the variable in the switch that was checking which image had been captured
switch (imageTaken) {
case "imageOne":
So adding the variable onSaveInstanceState and calling it appropriately onCreate solved the problem
@Override
public void onSaveInstanceState(Bundle bundle)
{
super.onSaveInstanceState(bundle);
bundle.putString("fileName", mTempPhotoPath);
bundle.putString("imageTaken", imageTaken);
}
If you find yourself here because of a similar issue, check that there is no variable that will not be initialized with a value incase your activity is recreated.
The upside is that the whole problem helped optimize my image sizes, and memory usage during bitmap processing