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

时间:2015-10-18 06:10:02

标签: java android bitmap

  

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

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

1 个答案:

答案 0 :(得分:2)

java.lang.OutOfMemoryError: 请求的大小超过了VM限制。 此错误表示Java应用程序尝试ti分配一个数组,其大小大于堆大小。

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

  

确认您的应用程序不存储不必要的信息。   仅存储和维护所需的那些信息   正确执行Java应用程序。

请阅读以下文件

  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