question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

saveAsBitmap with error

See original GitHub issue

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.

Issue Analytics

  • State:open
  • Created 2 years ago
  • Comments:35 (21 by maintainers)

github_iconTop GitHub Comments

1reaction
anderbytescommented, Oct 10, 2021

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.

1reaction
jserickskcommented, Oct 10, 2021

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

Read more comments on GitHub >

github_iconTop Results From Across the Web

Createimage giving error while saveasbitmap works - Inventor
I'm trying to show the view from inventor in another interface, but I can seem to find a way to use the createimage...
Read more >
Saving bitmap/"screenshot" of form not working when form is ...
When I run SaveAsBitmap by pressing the button, the form and its elements is correctly saved as a bmp file (just as if...
Read more >
SaveAsBMP Method - ComponentSource CDN
SaveAsBMP(String FileName, Short sImageIndex) ... When an error occurs and IfThrowException property is TRUE, an exception will be thrown.
Read more >
Can't save as bitmap - CorelDRAW Graphics Suite X8
How to disable the request to create a Corel account on every start of Corel Draw? Suggested Answer over 6 years ago. JoshStead....
Read more >
Export_EPS.java - ImageJ
cancelPressed()) return; saveAsBitmap = bd.yesPressed(); } width = ip. ... printStackTrace(); IJ.error("Error writing EPS file " + dir + name); } IJ.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found