java.lang.OutOfMemoryError in Bitmap.copy function



以为同一对象有两个副本,这可能是问题所在。但我不知道如何解决这个问题。请帮助我。提前致谢

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);
    }

日志猫

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: 请求的大小超出 VM 限制。此错误表示 Java 应用程序尝试分配大小大于堆大小的数组。

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

验证应用程序是否不存储不必要的信息。 仅存储和维护 正确执行 Java 应用程序。

请阅读以下文档

  1. http://developer.android.com/intl/es/training/displaying-bitmaps/load-bitmap.html
  2. 将图像加载到位图对象时出现奇怪的内存不足问题

最新更新