Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Canvas: trying to draw too large(256576512bytes) bitmap #2255

Open
samuelowino opened this issue Feb 19, 2022 · 3 comments
Open

Canvas: trying to draw too large(256576512bytes) bitmap #2255

samuelowino opened this issue Feb 19, 2022 · 3 comments

Comments

@samuelowino
Copy link

Fails in picasso:2.71828
Breaking point :PicassoDrawable.java line 47
Stacktrace:

Fatal Exception: java.lang.RuntimeException: Canvas: trying to draw too large(256576512bytes) bitmap.
       at android.graphics.RecordingCanvas.throwIfCannotDraw(RecordingCanvas.java:266)
       at android.graphics.BaseRecordingCanvas.drawBitmap(BaseRecordingCanvas.java:97)
       at android.graphics.drawable.BitmapDrawable.draw(BitmapDrawable.java:560)
       at com.squareup.picasso.PicassoDrawable.draw(PicassoDrawable.java:47)
       at android.widget.ImageView.onDraw(ImageView.java:1462)
       at android.view.View.draw(View.java:23893)
       at android.view.View.updateDisplayListIfDirty(View.java:22762)
       at android.view.View.draw(View.java:23623)
       at android.view.ViewGroup.drawChild(ViewGroup.java:5355)
       at android.view.ViewGroup.dispatchDraw(ViewGroup.java:5112)
       at android.view.View.updateDisplayListIfDirty(View.java:22748)
       at android.view.View.draw(View.java:23623)
       at android.view.ViewGroup.drawChild(ViewGroup.java:5355)
       at android.view.ViewGroup.dispatchDraw(ViewGroup.java:5112)
       at android.view.View.draw(View.java:23896)
       at android.view.View.updateDisplayListIfDirty(View.java:22762)
       at android.view.View.draw(View.java:23623)
       at android.view.ViewGroup.drawChild(ViewGroup.java:5355)
       at androidx.recyclerview.widget.RecyclerView.drawChild(RecyclerView.java)
       at android.view.ViewGroup.dispatchDraw(ViewGroup.java:5112)
       at android.view.View.draw(View.java:23896)
       at androidx.core.view.ViewCompat.postInvalidateOnAnimation(ViewCompat.java)
       at android.view.View.updateDisplayListIfDirty(View.java:22762)
       at android.view.View.draw(View.java:23623)
       at android.view.ViewGroup.drawChild(ViewGroup.java:5355)
       at android.view.ViewGroup.dispatchDraw(ViewGroup.java:5112)
       at android.view.View.draw(View.java:23896)
       at android.widget.HorizontalScrollView.draw(HorizontalScrollView.java:2238)
       at android.view.View.updateDisplayListIfDirty(View.java:22762)
       at android.view.View.draw(View.java:23623)
       at android.view.ViewGroup.drawChild(ViewGroup.java:5355)
       at android.view.ViewGroup.dispatchDraw(ViewGroup.java:5112)
       at android.view.View.draw(View.java:23896)
       at android.view.View.updateDisplayListIfDirty(View.java:22762)
       at android.view.View.draw(View.java:23623)
       at android.view.ViewGroup.drawChild(ViewGroup.java:5355)
       at android.view.ViewGroup.dispatchDraw(ViewGroup.java:5112)
       at android.view.View.updateDisplayListIfDirty(View.java:22748)
       at android.view.View.draw(View.java:23623)
       at android.view.ViewGroup.drawChild(ViewGroup.java:5355)
       at android.view.ViewGroup.dispatchDraw(ViewGroup.java:5112)
       at android.view.View.draw(View.java:23896)
       at androidx.core.view.ViewCompat.postInvalidateOnAnimation(ViewCompat.java)
       at android.view.View.updateDisplayListIfDirty(View.java:22762)
       at android.view.View.draw(View.java:23623)
       at android.view.ViewGroup.drawChild(ViewGroup.java:5355)
       at android.view.ViewGroup.dispatchDraw(ViewGroup.java:5112)
       at android.view.View.updateDisplayListIfDirty(View.java:22748)
       at android.view.View.draw(View.java:23623)
       at android.view.ViewGroup.drawChild(ViewGroup.java:5355)
       at androidx.coordinatorlayout.widget.CoordinatorLayout.drawChild(CoordinatorLayout.java:13)
       at android.view.ViewGroup.dispatchDraw(ViewGroup.java:5112)
       at android.view.View.draw(View.java:23896)
       at android.view.View.updateDisplayListIfDirty(View.java:22762)
       at android.view.View.draw(View.java:23623)
       at android.view.ViewGroup.drawChild(ViewGroup.java:5355)
       at android.view.ViewGroup.dispatchDraw(ViewGroup.java:5112)
       at android.view.View.updateDisplayListIfDirty(View.java:22748)
       at android.view.View.draw(View.java:23623)
       at android.view.ViewGroup.drawChild(ViewGroup.java:5355)
       at android.view.ViewGroup.dispatchDraw(ViewGroup.java:5112)
       at android.view.View.updateDisplayListIfDirty(View.java:22748)
       at android.view.View.draw(View.java:23623)
       at android.view.ViewGroup.drawChild(ViewGroup.java:5355)
       at android.view.ViewGroup.dispatchDraw(ViewGroup.java:5112)
       at android.view.View.updateDisplayListIfDirty(View.java:22748)
       at android.view.View.draw(View.java:23623)
       at android.view.ViewGroup.drawChild(ViewGroup.java:5355)
       at android.view.ViewGroup.dispatchDraw(ViewGroup.java:5112)
       at android.view.View.updateDisplayListIfDirty(View.java:22748)
       at android.view.View.draw(View.java:23623)
       at android.view.ViewGroup.drawChild(ViewGroup.java:5355)
       at android.view.ViewGroup.dispatchDraw(ViewGroup.java:5112)
       at android.view.View.draw(View.java:23896)
       at com.android.internal.policy.DecorView.draw(DecorView.java:1337)
       at android.view.View.updateDisplayListIfDirty(View.java:22762)
       at android.view.ThreadedRenderer.updateViewTreeDisplayList(ThreadedRenderer.java:602)
       at android.view.ThreadedRenderer.updateRootDisplayList(ThreadedRenderer.java:608)
       at android.view.ThreadedRenderer.draw(ThreadedRenderer.java:684)
       at android.view.ViewRootImpl.draw(ViewRootImpl.java:5305)
       at android.view.ViewRootImpl.performDraw(ViewRootImpl.java:5013)
       at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:4106)
       at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:2831)
       at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:10311)
       at android.view.Choreographer$CallbackRecord.run(Choreographer.java:1063)
       at android.view.Choreographer.doCallbacks(Choreographer.java:845)
       at android.view.Choreographer.doFrame(Choreographer.java:780)
       at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:1048)
       at android.os.Handler.handleCallback(Handler.java:938)
       at android.os.Handler.dispatchMessage(Handler.java:99)
       at android.os.Looper.loopOnce(Looper.java:226)
       at android.os.Looper.loop(Looper.java:313)
       at android.app.ActivityThread.main(ActivityThread.java:8641)
       at java.lang.reflect.Method.invoke(Method.java)
       at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:567)
       at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1133)

I expected Picasso to compress the image incase it was too large or atleast fail silently.

Thanks in advance.

@Hamahmi
Copy link

Hamahmi commented Mar 24, 2022

use .fit()

@JasurbekA
Copy link

JasurbekA commented Mar 28, 2022

@Hamahmi Using fit() will affect the images aspect ratio
@samuelowino A better way is to write your own Transformation based on your needs, in my case width is more important

class ScaleToFitWidthHeightTransformation(
  private val targetWidth: Int,
  private val uniqueKey: String
) : Transformation {
  override fun transform(source: Bitmap): Bitmap {
    val scale: Float = targetWidth.toFloat() / source.width
    val targetHeight: Int = (source.height * scale).roundToInt()
    val scaleBitmap: Bitmap = Bitmap.createScaledBitmap(source, targetWidth, targetHeight, true)
    if (scaleBitmap != source) source.recycle()
    return scaleBitmap
  }

  override fun key(): String {
    return uniqueKey
  }
}

Here I will share my implementation based on this gist

@dceo300
Copy link

dceo300 commented Oct 20, 2023

  • [](

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants