Java android位图内存不足错误
我正在使用位图。它抛出内存不足错误(5次中有2次)。 如何避免呢。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
以下是我的代码: 在这一行中,整个位图将填充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){
}