I am trying to make a simple application which uses the system camera application to take picture by passing camera intent, stores the image in file system, then takes the image and converts it into bitmap and display it in image view by performing some scale down on it. I have tested my app in android version 6 and I get the following error:
java.lang.RuntimeException:
at android.app.ActivityThread.performResumeActivity (ActivityThread.java:3273)
at android.app.ActivityThread.handleResumeActivity (ActivityThread.java:3304)
at android.app.ActivityThread.handleLaunchActivity (ActivityThread.java:2618)
at android.app.ActivityThread.-wrap11 (ActivityThread.java)
at android.app.ActivityThread$H.handleMessage (ActivityThread.java:1469)
at android.os.Handler.dispatchMessage (Handler.java:111)
at android.os.Looper.loop (Looper.java:207)
at android.app.ActivityThread.main (ActivityThread.java:5692)
at java.lang.reflect.Method.invoke (Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run (ZygoteInit.java:905)
at com.android.internal.os.ZygoteInit.main (ZygoteInit.java:766)
Caused by: java.lang.RuntimeException:
at android.app.ActivityThread.deliverResults (ActivityThread.java:3888)
at android.app.ActivityThread.performResumeActivity (ActivityThread.java:3255)
Caused by: java.lang.ArithmeticException:
at iforest.photogps.MainActivity.previewCapturedImage (MainActivity.java:241)
at iforest.photogps.MainActivity.onActivityResult (MainActivity.java:147)
at android.app.Activity.dispatchActivityResult (Activity.java:6461)
at android.app.ActivityThread.deliverResults (ActivityThread.java:3884)
The app gets ArithmeticException in line 241 inside previewCapturedImage for scaleFactor:
/* preview the image on app's screen*/
private Bitmap previewCapturedImage() {
// Get the dimensions of the View
int targetW = imageView.getWidth();
int targetH = imageView.getHeight();
// Get the dimensions of the bitmap
BitmapFactory.Options bmOptions = new BitmapFactory.Options();
bmOptions.inJustDecodeBounds = true;
BitmapFactory.decodeFile(mCurrentPhotoPath, bmOptions);
int photoW = bmOptions.outWidth;
int photoH = bmOptions.outHeight;
// Determine how much to scale down the image
int scaleFactor = Math.min(photoW/targetW, photoH/targetH); <-- line 241
// Decode the image file into a Bitmap sized to fill the View
bmOptions.inJustDecodeBounds = false;
bmOptions.inSampleSize = scaleFactor;
bmOptions.inPurgeable = true;
//Bitmap bitmap = BitmapFactory.decodeFile(mCurrentPhotoPath, bmOptions);
//imageView.setImageBitmap(bitmap);
return BitmapFactory.decodeFile(mCurrentPhotoPath, bmOptions);
}
The report does not give me enough information about the arithmetic exception. May the problem be the targetW or targetH are 0?