java.lang.RuntimeException: Canvas: trying to draw

2019-08-02 10:36发布

问题:

I am trying to load image with glide, it is working fine in Android 6.0, but it is not working in greater than Android 6.0. I searched, but did not find any solution. I also increased heap size in manifext.xml. I also tried with Fresco and Picasso, but it is always occurring this exception. I also tried to reduce the size of picture with help of bitmap. This is the log detail.

Increasing code cache capacity to 1024KB
05-28 23:24:46.255 9111-9111/com.romilandroid.shreekshatriya E/AndroidRuntime: FATAL EXCEPTION: main
    Process: com.romilandroid.shreekshatriya, PID: 9111
    java.lang.RuntimeException: Canvas: trying to draw too large(144000000bytes) bitmap.
        at android.view.DisplayListCanvas.throwIfCannotDraw(DisplayListCanvas.java:229)
        at android.view.RecordingCanvas.drawBitmap(RecordingCanvas.java:97)
        at android.graphics.drawable.BitmapDrawable.draw(BitmapDrawable.java:529)
        at android.widget.ImageView.onDraw(ImageView.java:1367)
        at android.view.View.draw(View.java:19123)
        at android.view.View.updateDisplayListIfDirty(View.java:18073)
        at android.view.View.draw(View.java:18851)
        at android.view.ViewGroup.drawChild(ViewGroup.java:4214)
        at android.view.ViewGroup.dispatchDraw(ViewGroup.java:4000)
        at android.view.View.draw(View.java:19126)
        at android.view.View.updateDisplayListIfDirty(View.java:18073)
        at android.view.View.draw(View.java:18851)
        at android.view.ViewGroup.drawChild(ViewGroup.java:4214)
        at android.view.ViewGroup.dispatchDraw(ViewGroup.java:4000)
        at android.view.View.draw(View.java:19126)
        at android.support.v4.view.ViewPager.draw(ViewPager.java:2420)
        at android.view.View.updateDisplayListIfDirty(View.java:18073)
        at android.view.View.draw(View.java:18851)
        at android.view.ViewGroup.drawChild(ViewGroup.java:4214)
        at android.view.ViewGroup.dispatchDraw(ViewGroup.java:4000)
        at android.view.View.updateDisplayListIfDirty(View.java:18064)
        at android.view.View.draw(View.java:18851)
        at android.view.ViewGroup.drawChild(ViewGroup.java:4214)
        at android.view.ViewGroup.dispatchDraw(ViewGroup.java:4000)
        at android.view.View.updateDisplayListIfDirty(View.java:18064)
        at android.view.View.draw(View.java:18851)
        at android.view.ViewGroup.drawChild(ViewGroup.java:4214)
        at android.view.ViewGroup.dispatchDraw(ViewGroup.java:4000)
        at android.view.View.updateDisplayListIfDirty(View.java:18064)
        at android.view.View.draw(View.java:18851)
        at android.view.ViewGroup.drawChild(ViewGroup.java:4214)
        at android.view.ViewGroup.dispatchDraw(ViewGroup.java:4000)
        at android.view.View.updateDisplayListIfDirty(View.java:18064)
        at android.view.View.draw(View.java:18851)
        at android.view.ViewGroup.drawChild(ViewGroup.java:4214)
        at android.view.ViewGroup.dispatchDraw(ViewGroup.java:4000)
        at android.view.View.updateDisplayListIfDirty(View.java:18064)
        at android.view.View.draw(View.java:18851)
        at android.view.ViewGroup.drawChild(ViewGroup.java:4214)
        at android.view.ViewGroup.dispatchDraw(ViewGroup.java:4000)
        at android.view.View.updateDisplayListIfDirty(View.java:18064)
        at android.view.View.draw(View.java:18851)
        at android.view.ViewGroup.drawChild(ViewGroup.java:4214)
        at android.view.ViewGroup.dispatchDraw(ViewGroup.java:4000)
        at android.view.View.draw(View.java:19126)
        at com.android.internal.policy.DecorView.draw(DecorView.java:785)
        at android.view.View.updateDisplayListIfDirty(View.java:18073)
        at android.view.ThreadedRenderer.updateViewTreeDisplayList(ThreadedRenderer.java:643)
        at android.view.ThreadedRenderer.updateRootDisplayList(ThreadedRenderer.java:649)
        at android.view.ThreadedRenderer.draw(ThreadedRenderer.java:757)
        at android.view.ViewRootImpl.draw(ViewRootImpl.java:2980)
        at android.view.ViewRootImpl.performDraw(ViewRootImpl.java:2794)
        at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:2347)
        at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1386)
        at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:6733)
        at android.view.Choreographer$CallbackRecord.run(Choreographer.java:911)
        at android.view.Choreographer.doCallbacks(Choreographer.java:723)
        at android.view.Choreographer.doFrame(Choreographer.java:658)
        at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:897)
        at android.os.Handler.handleCallback(Handler.java:789)
        at android.os.Handler.dispatchMessage(Handler.java:98)
        at android.os.Looper.loop(Looper.java:164)
        at android.app.ActivityThread.main(ActivityThread.java:6541)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:240)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:767)

回答1:

 Glide.with(mContext)
                .load("your image drawable")
                .error(android.R.drawable.ic_dialog_alert)
                .override(320, 210)
                .listener(new RequestListener<String, GlideDrawable>() {
                    @Override
                    public boolean onException(Exception e, String model, Target<GlideDrawable> target, boolean isFirstResource) {                           
                        return false;
                    }
                    @Override
                    public boolean onResourceReady(GlideDrawable resource, String model, Target<GlideDrawable> target, boolean isFromMemoryCache, boolean isFirstResource) {                          
                        return false;
                    }
                })
                .into(holder.ModelImage);

Use .overrride with glide to load the larger image. Becuase the image you are trying to load have large size . And also you can do one more thing that is :

 <application
        android:largeHeap="true">
</applicatino>

into the android app manifest for the working of the app fine with a large heap size. Reply if you found any error. Thanks



回答2:

try this in your build.gradle

dependencies {
    implementation ("com.github.bumptech.glide:glide:4.7.1") {
        exclude group: "com.android.support"
    } 
    implementation "com.android.support:support-fragment:26.1.0"
}


回答3:

Try This It helps You

  • In my case - problem was only on Sumsung devices with Android 7, and problem was in splash screen proportions. after changing height to 1024 px - everything waork fine.

  • Move your image in the drawable to mipmap-xxhdpi.Your image is in bitmap format so you should put your image in mipmap folder,then it will work

  • Open your drawable folder check all images size and changing the size your images it work for me.



回答4:

If you are loading the image from your project (not from the web), you should place your image inside "mipmap folder" and not in "drawable folder" because the image is in bitmap format.