简体   繁体   中英

java.lang.ArithmeticException: divide by zero when compress image from pick gallery

I'm getting error java.lang.ArithmeticException: divide by zero when compress image from pick gallery. this Is my code:

 public String compressImage(String filePath, String outputFilename) {

        Bitmap scaledBitmap = null;

        BitmapFactory.Options options = new BitmapFactory.Options();

        options.inJustDecodeBounds = true;
        Bitmap bmp = BitmapFactory.decodeFile(filePath, options);

        int actualHeight = options.outHeight;
        int actualWidth = options.outWidth;
        Log.i("INFO", "ActualHeight Input: " + actualHeight);
        Log.i("INFO", "ActualWidth Input: " + actualWidth);

        float maxHeight = 816.0f;
        float maxWidth = 612.0f;
        float imgRatio = actualWidth / actualHeight;
        float maxRatio = maxWidth / maxHeight;


       ............

        return outputFilename;
    }

the error get in lin : float imgRatio = actualWidth / actualHeight; so how to fix it?

The fact that you are getting the exception says that division by zero is occurring, and that options.outHeight is returning zero. You need to find out why that is happening. If Log.i("INFO", "ActualHeight Input: " + actualHeight); returns you something other than null , provide us more details.

Please add the below line on your manifest (inside the Application block).

Android: requestLegacyExternalStorage = "true"

Hope, this will be helpful on this issue.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM