Getting Bitmap from WebView generates OutOfMemory crash

后端 未结 2 1619
长情又很酷
长情又很酷 2021-02-19 11:18

I have a custom WebView and I want to get a bitmap of its content (offscreen included). I used this code, which I got from here:

 public static Bitm         


        
相关标签:
2条回答
  • 2021-02-19 11:27

    Quick fix:-

    Set android:largeHeap="true" in your AndroidManifest.xml file.

    <application
        android:name="yourapplication"
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher_rounded"
        android:label="@string/app_name"
        android:largeHeap="true"
        android:roundIcon="@mipmap/ic_launcher_rounded"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
    

    Gud luck :)

    0 讨论(0)
  • 2021-02-19 11:33

    Okay, I've cracked this one meself. The problem indeed was in the fact that the function returned the Bitmap waay before the zooming out procedure was over, so I had to delay it.

    I incorporated

    Handler handler = new Handler();
            handler.postDelayed(new Runnable() {
                @Override
                public void run() {
                    //bulk of my code
                }
            }, 1000);
    

    which forced to change the structure of the function from public Bitmap to public void. Here's directly related thread. Despite some minor tweaks I still need to fix, the approach works like a charm

    0 讨论(0)
提交回复
热议问题