android位图中的内存不足错误

use*_*306 3 java android bitmap out-of-memory

我正在使用位图.它会抛出内存错误(5次中有2次).怎么可以避免.

以下是我的代码:

  bitmap = android.provider.MediaStore.Images.Media.getBitmap(cr, imageUri);
  photo_new= rotateImage(bitmap, 90);
  ByteArrayOutputStream stream = new ByteArrayOutputStream();

  photo_new.compress(Bitmap.CompressFormat.JPEG, 100, stream);
  byte[] byteArray = stream.toByteArray();

  Intent i = new Intent(getApplicationContext(),new_class.class);
  i.putExtra("image", byteArray);

  startActivity(i);
  byteArray=null;
Run Code Online (Sandbox Code Playgroud)

Kau*_*hik 5

你得到的是OutOfMemoryError,因为你没有使用过的recycle 那些bitmaps

在你使用它们之后试试recycle那些bitmaps

bitmap = android.provider.MediaStore.Images.Media.getBitmap(cr, imageUri);
  photo_new= rotateImage(bitmap, 90);
  ByteArrayOutputStream stream = new ByteArrayOutputStream();

  photo_new.compress(Bitmap.CompressFormat.JPEG, 100, stream);
  byte[] byteArray = stream.toByteArray();
  bitmap.recycle();
  Intent i = new Intent(getApplicationContext(),new_class.class);
  i.putExtra("image", byteArray);

  startActivity(i);
  byteArray=null;
Run Code Online (Sandbox Code Playgroud)