Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/204.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/firebase/6.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java android位图内存不足错误_Java_Android_Bitmap_Out Of Memory - Fatal编程技术网

Java android位图内存不足错误

Java android位图内存不足错误,java,android,bitmap,out-of-memory,Java,Android,Bitmap,Out Of Memory,我正在使用位图。它抛出内存不足错误(5次中有2次)。 如何避免呢。 以下是我的代码: 在这一行中,整个位图将填充ram。将位图质量从100更改为50-60,如下所示 photo_new.compress(Bitmap.CompressFormat.JPEG, 50, stream); 或 尝试这两种方法并查看结果。您得到的是因为您没有回收 您使用的位图 使用完位图后,尝试回收这些位图 bitmap = android.provider.MediaStore.Images.Media.getB

我正在使用位图。它抛出内存不足错误(5次中有2次)。 如何避免呢。

以下是我的代码:

在这一行中,整个位图将填充ram。将位图质量从100更改为
50-60
,如下所示

 photo_new.compress(Bitmap.CompressFormat.JPEG, 50, stream);

尝试这两种方法并查看结果。

您得到的是因为您没有
回收
您使用的
位图

使用完位图后,尝试
回收
这些位图

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;
错误的 1) 。位图质量很高

2) 。您没有使用try-catch

建议 1) 。将位图的质量降低到45-50

2) 。使用try-catch块防止应用程序崩溃

解决方案//发送方活动 接收器活动
它可以在不同的图像上正常工作?有什么区别。它在哪一点抛出内存错误?这些位图的大小是多少?它在以下位置抛出错误:
bitmap=android.provider.MediaStore.Images.Media.getBitmap(cr,imageUri)我使用上面的代码没有任何修改,但不幸的是,第一行本身给了我内存不足的错误。MediaStore.Images.Media.getBitmap(cr,imageUri)//在这一行中,它是崩溃的@AnbarasuChinna
 photo_new.compress(Bitmap.CompressFormat.JPEG, 50, stream);
 photo_new.compress(Bitmap.CompressFormat.JPEG, 60, stream);
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;
try{
     Intent _intent = new Intent(this, newscreen.class);
     Bitmap _bitmap; // your bitmap
     ByteArrayOutputStream _bs = new ByteArrayOutputStream();
     _bitmap.compress(Bitmap.CompressFormat.PNG, 50, _bs);
     i.putExtra("byteArray", _bs.toByteArray());
     startActivity(i);
}catch(Exception e){
}
try{
     if(getIntent().hasExtra("byteArray")) {

     ImageView _imv= new ImageView(this);
     Bitmap _bitmap = BitmapFactory.decodeByteArray(getIntent().getByteArrayExtra("byteArray"),0,getIntent().getByteArrayExtra("byteArray").length);        
    _imv.setImageBitmap(_bitmap);
 }
}catch(Exception e){
 }