PhotoEditor: saveAsBitmap with error

I’m trying to execute saveAsBitmap but I often get this error:

E/ImageFilterView: onDrawFrame: android.graphics.Bitmap@de42880 E/PhotoEditorView: saveFilter: android.graphics.Bitmap@de42880 D/PhotoEditorView: onBitmapLoaded() called with: sourceBitmap = [android.graphics.Bitmap@de42880] onBitmapLoaded() called with: sourceBitmap = [android.graphics.Bitmap@de42880] E/AndroidRuntime: FATAL EXCEPTION: AsyncTask #1 Process: com.company.myapp, PID: 24958 java.lang.RuntimeException: An error occurred while executing doInBackground() at android.os.AsyncTask$3.done(AsyncTask.java:353) at java.util.concurrent.FutureTask.finishCompletion(FutureTask.java:383) at java.util.concurrent.FutureTask.setException(FutureTask.java:252) at java.util.concurrent.FutureTask.run(FutureTask.java:271) at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:245) at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1162) at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:636) at java.lang.Thread.run(Thread.java:764) Caused by: java.lang.IndexOutOfBoundsException: Index: 1, Size: 0 at java.util.ArrayList.get(ArrayList.java:437) at android.view.ViewGroup.getAndVerifyPreorderedView(ViewGroup.java:3530) at android.view.ViewGroup.dispatchDraw(ViewGroup.java:4001) at android.view.View.draw(View.java:19003) at android.view.ViewGroup.drawChild(ViewGroup.java:4218) at android.view.ViewGroup.dispatchDraw(ViewGroup.java:4004) at android.view.View.draw(View.java:19138) at ja.burhanrashid52.photoeditor.PhotoSaverTask.captureView(PhotoSaverTask.java:161) at ja.burhanrashid52.photoeditor.PhotoSaverTask.buildBitmap(PhotoSaverTask.java:104) at ja.burhanrashid52.photoeditor.PhotoSaverTask.saveImageAsBitmap(PhotoSaverTask.java:76) at ja.burhanrashid52.photoeditor.PhotoSaverTask.doInBackground(PhotoSaverTask.java:68) at ja.burhanrashid52.photoeditor.PhotoSaverTask.doInBackground(PhotoSaverTask.java:23) at android.os.AsyncTask$2.call(AsyncTask.java:333) at java.util.concurrent.FutureTask.run(FutureTask.java:266) at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:245)  at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1162)  at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:636)  at java.lang.Thread.run(Thread.java:764)

my code is as simple as:

val onSaveBMP: OnSaveBitmap = object: OnSaveBitmap {
                    override fun onBitmapReady(saveBitmap: Bitmap?) { }
                    override fun onFailure(e: Exception?) { }
                }

                mPhoto.saveAsBitmap(onSaveBMP)

really don’t know what is happenin. At first I thought it was something at the moment I tried to use the saveBitmap object, but no… even this code above is causing the crash.

very weird. It is as if the Bitmap can’t be generated for some reason.

About this issue

  • Original URL
  • State: closed
  • Created 3 years ago
  • Comments: 38 (24 by maintainers)

Commits related to this issue

Most upvoted comments

I think I will be able to keep old API. I’ll update the code and create PR.

Well we really were experiencing different issues, after all. Happy that you found your fix. Mine maybe sometime will come, problably when the native libraries of Android handle better the bitmap inside the canvas method.

I fix the problem. Well, not directly in the library. I’m using my own way to save the image, capturing the photoEditorView view directly. I saw that many faced the same problem (Software rendering doesn’t support hardware bitmaps), so I’ll leave here the solution that worked for me. I believe the captureView method in the library should be modified to fix the problem directly, but I haven’t tried to convert the code to Java since I’m using Kotlin.

Method to capture view:


    fun captureView(view: View, window: Window, bitmapCallback: (Bitmap) -> Unit) {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            // Above Android O, use PixelCopy
            val bitmap = Bitmap.createBitmap(view.width, view.height, Bitmap.Config.ARGB_8888)
            val location = IntArray(2)
            view.getLocationInWindow(location)
            PixelCopy.request(
                window,
                Rect(location[0], location[1], location[0] + view.width, location[1] + view.height),
                bitmap,
                {
                    if (it == PixelCopy.SUCCESS) {
                        bitmapCallback.invoke(bitmap)
                    }
                },
                Handler(Looper.getMainLooper())
            )
        } else {
            val bitmap = Bitmap.createBitmap(
                view.width, view.height, Bitmap.Config.RGB_565
            )
            val canvas = Canvas(bitmap)
            view.draw(canvas)
            canvas.setBitmap(null)
            bitmapCallback.invoke(bitmap)
        }
    }

So to save the image:

photoEditor.clearHelperBox()
captureView(photoEditorView, window) { bitmap ->
// Operation to save the image normally with Bitmap
}

Reference: https://stackoverflow.com/questions/58314397/java-lang-illegalstateexception-software-rendering-doesnt-support-hardware-bit

New https://github.com/burhanrashid52/PhotoEditor/pull/381 to let the user use a SYNC save instead of the default one, if wanted.