0

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?

ColdFire
  • 6,764
  • 6
  • 35
  • 51
Thanos Infosec
  • 57
  • 1
  • 10

1 Answers1

0

Yes, ArithmeticException can be thrown when you divide by zero. Put a breakpoint or Log the values of targetH and targetW. It is probably because you are getting the height and width of the imageView before it is measured. You can try moving these lines:

int targetW = imageView.getWidth();
int targetH = imageView.getHeight();

Or the whole method call to onWindowFocusChanged. Or if all else fails, this answer worked around it by extending ImageView.

Suleyman
  • 2,765
  • 2
  • 18
  • 31
  • what about to add and if check before scalefactor checking if targetW or targetH are zero, then make them equal to 1. Is this way image be displayed? – Thanos Infosec May 04 '18 at 11:11
  • @Thanos but your `imageView` height and width are not 1, why would you want to set them to 1? It will work, because you will eliminate division by 0, but you may get some weird results, because instead of the real size of the `imageView` you will have 1. Another option may be to set a fixed size for your `imageView` in XML, and then just hardcode it in your class, instead of getting it with `imageView.getHeight()`. The best way is to try and see :) – Suleyman May 04 '18 at 13:36