steelkiwi / cropiwa

📐 Configurable Custom Crop widget for Android
http://steelkiwi.com/
2.23k stars 331 forks source link

x/y must be >= 0 #11

Open martintoften opened 7 years ago

martintoften commented 7 years ago
FATAL EXCEPTION: AsyncTask #3
                                                                        java.lang.RuntimeException: An error occurred while executing doInBackground()
                                                                            at android.os.AsyncTask$3.done(AsyncTask.java:325)
                                                                            at java.util.concurrent.FutureTask.finishCompletion(FutureTask.java:354)
                                                                            at java.util.concurrent.FutureTask.setException(FutureTask.java:223)
                                                                            at java.util.concurrent.FutureTask.run(FutureTask.java:242)
                                                                            at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:243)
                                                                            at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1133)
                                                                            at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:607)
                                                                            at java.lang.Thread.run(Thread.java:761)
                                                                         Caused by: java.lang.IllegalArgumentException: x must be >= 0
                                                                            at android.graphics.Bitmap.checkXYSign(Bitmap.java:395)
                                                                            at android.graphics.Bitmap.createBitmap(Bitmap.java:731)
                                                                            at android.graphics.Bitmap.createBitmap(Bitmap.java:701)
                                                                            at com.steelkiwi.cropiwa.image.CropArea.applyCropTo(CropArea.java:35)
                                                                            at com.steelkiwi.cropiwa.image.CropImageTask.doInBackground(CropImageTask.java:49)
                                                                            at com.steelkiwi.cropiwa.image.CropImageTask.doInBackground(CropImageTask.java:20)
                                                                            at android.os.AsyncTask$2.call(AsyncTask.java:305)
                                                                            at java.util.concurrent.FutureTask.run(FutureTask.java:237)
                                                                            at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:243) 
                                                                            at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1133) 
                                                                            at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:607) 
                                                                            at java.lang.Thread.run(Thread.java:761) 

I have no way to catch this error :( Can this be solved by catching the IllegalArgumentException in the AsyncTask, return it and call the onError method?

smilecs commented 7 years ago

I will suppose x to be width? have you tried checking what width is returned so compare and see first?

longpham2310 commented 7 years ago

I make it workaround like this.

public Bitmap applyCropTo(Bitmap bitmap) {
        int x = findRealCoordinate(bitmap.getWidth(), cropRect.left, imageRect.width());
        int y = findRealCoordinate(bitmap.getHeight(), cropRect.top, imageRect.height());
        int width = findRealCoordinate(bitmap.getWidth(), cropRect.width(), imageRect.width());
        int height = findRealCoordinate(bitmap.getHeight(), cropRect.height(), imageRect.height());

        if (x < 0) {
            x = 0;
        }
        if (y + height >= bitmap.getHeight()) {
            y = bitmap.getHeight() - height;
        }

        Log.d(TAG, "x/y/with/height : " + x + "/" + y + "/" + width + "/" + height);
        Bitmap immutableCropped = Bitmap.createBitmap(bitmap,
                x,
                y,
                width,
                height);
        return immutableCropped.copy(immutableCropped.getConfig(), true);
    }
zhangzhichaolove commented 7 years ago

CropImageTask -> doInBackground -> IOException changed to catch (Exception e) {              Return e;          }

CropArea -> applyCropTo (Bitmap bitmap) -> changed to public Bitmap applyCropTo (Bitmap bitmap) throws IllegalArgumentException

Above can prevent crashes.

CropIwaView -> preceded by judgment: crop (CropIwaSaveConfig saveConfig) {      If (imageView.getImageRect (). Left == -1 || imageView.getImageRect (). Left == 0) {              Return;          } } If the crop method has not yet completed, it will be returned, of course, here you can do other operating logic.

flasher297 commented 7 years ago

I've fixed this issue with posting message on UI thread. It seems that this exception is raised when sixe of view is still not set.

 cropView.post(() ->
                cropView.crop(new CropIwaSaveConfig.Builder(croppedFileDestination)
                        .setCompressFormat(Bitmap.CompressFormat.JPEG)
                        .setQuality(80)
                        .build()));