Bitmap.copy 函数中的 java.lang.OutOfMemoryError

标签 java android bitmap

I thought there is two copies of same object.that may be the issue.but I do not know how to solve that.please help me.Thanks in advance

protected void onDraw(Canvas canvas) {

        Drawable drawable = getDrawable();

        if (drawable == null) {
            return;
        }

        if (getWidth() == 0 || getHeight() == 0) {
            return;
        }
        Bitmap b = ((BitmapDrawable) drawable).getBitmap();
// I got error in this line.
        Bitmap bitmap = b.copy(Bitmap.Config.ARGB_8888, true);

        int w = getWidth(), h = getHeight();

        Bitmap roundBitmap = getRoundedCroppedBitmap(bitmap, w);
        canvas.drawBitmap(roundBitmap, 0, 0, null);

    }

logcat

10-18 11:04:33.208 32110-32110/com.example.rajitha.myapplication E/AndroidRuntime: FATAL EXCEPTION: main
10-18 11:04:33.208 32110-32110/com.example.rajitha.myapplication E/AndroidRuntime: java.lang.OutOfMemoryError
10-18 11:04:33.208 32110-32110/com.example.rajitha.myapplication E/AndroidRuntime:     at android.graphics.Bitmap.nativeCopy(Native Method)
10-18 11:04:33.208 32110-32110/com.example.rajitha.myapplication E/AndroidRuntime:     at android.graphics.Bitmap.copy(Bitmap.java:403)
10-18 11:04:33.208 32110-32110/com.example.rajitha.myapplication E/AndroidRuntime:     at com.example.rajitha.myapplication.RoundedImageView.onDraw(RoundedImageView.java:39)
10-18 11:04:33.208 32110-32110/com.example.rajitha.myapplication E/AndroidRuntime:     at android.view.View.draw(View.java:11054)
10-18 11:04:33.208 32110-32110/com.example.rajitha.myapplication E/AndroidRuntime:     at android.view.View.getDisplayList(View.java:10493)
10-18 11:04:33.208 32110-32110/com.example.rajitha.myapplication E/AndroidRuntime:     at android.view.ViewGroup.drawChild(ViewGroup.java:2958)
10-18 11:04:33.208 32110-32110/com.example.rajitha.myapplication E/AndroidRuntime:     at android.view.ViewGroup.dispatchDraw(ViewGroup.java:2596)
10-18 11:04:33.208 32110-32110/com.example.rajitha.myapplication E/AndroidRuntime:     at android.view.View.getDisplayList(View.java:10491)
10-18 11:04:33.208 32110-32110/com.example.rajitha.myapplication E/AndroidRuntime:     at android.view.ViewGroup.drawChild(ViewGroup.java:2958)
10-18 11:04:33.208 32110-32110/com.example.rajitha.myapplication E/AndroidRuntime:     at android.view.ViewGroup.dispatchDraw(ViewGroup.java:2596)
10-18 11:04:33.208 32110-32110/com.example.rajitha.myapplication E/AndroidRuntime:     at android.view.View.draw(View.java:11057)
10-18 11:04:33.208 32110-32110/com.e

最佳答案

java.lang.OutOfMemoryError: 请求的大小超出虚拟机限制。 此错误表明 Java 应用程序尝试分配一个数组,该数组的大小大于堆大小。

OutOfMemoryError 扩展了 VirtualMachineError 类,它表示 JVM 损坏,或者资源耗尽而无法运行。

Verify that your application does not store unnecessary information. Store and maintain only those pieces of information required for the proper execution of your Java application.

请阅读以下文档

  1. http://developer.android.com/intl/es/training/displaying-bitmaps/load-bitmap.html
  2. Strange out of memory issue while loading an image to a Bitmap object

关于Bitmap.copy 函数中的 java.lang.OutOfMemoryError,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33194896/

相关文章:

android - Kotlin 中的方法 hashMapOf()

android - 我可以在 Android Studio 中测试响应式网站吗?如何?

android - Xamarin 和 Android - 退出方法时崩溃

Android 在 fragment 之间传递位图

c - 用 C 语言编写位图

android ndk,如何将图像文件读取到位图jobject?

java - 如何将 HBox 中的子元素左对齐、居中对齐和右对齐

java - 如何在 Java 中比较字符串?

java - Acceleo 和 Xpand 有什么区别?

java - WebSphere:boundedbuffer.waitGet_ 占用了大部分 CPU