朋友,
我有一个页面,我正在显示图库中的图像然后我有下一页按钮移动到该按钮上的下一个活动我正在使用clearBitmap(); 释放它所使用的记忆。
私有位图位图;
oncreate()
{
_image = (ImageView)findViewById(R.id.MyImage);
_path = getRealPathFromURI(_data.getData());
BitmapFactory.Options options = new BitmapFactory.Options();
options.inSampleSize = 2;
bitmap = BitmapFactory.decodeFile( _path, options );
_image.setImageBitmap(bitmap);
next.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View view) {
Intent myIntent = new Intent(UploadImageOnly.this, ImageGallery.class);
startActivity(myIntent);
clearBitmap();
}
这给了我错误 logcat的
08-13 12:07:07.649: ERROR/AndroidRuntime(753): Uncaught handler: thread main exiting due to uncaught exception
08-13 12:07:07.869: ERROR/AndroidRuntime(753): java.lang.RuntimeException: Canvas: trying to use a recycled bitmap android.graphics.Bitmap@43c13dd0
08-13 12:07:07.869: ERROR/AndroidRuntime(753): at android.graphics.Canvas.throwIfRecycled(Canvas.java:955)
08-13 12:07:07.869: ERROR/AndroidRuntime(753): at android.graphics.Canvas.drawBitmap(Canvas.java:1044)
08-13 12:07:07.869: ERROR/AndroidRuntime(753): at android.graphics.drawable.BitmapDrawable.draw(BitmapDrawable.java:323)
08-13 12:07:07.869: ERROR/AndroidRuntime(753): at android.widget.ImageView.onDraw(ImageView.java:845)
08-13 12:07:07.869: ERROR/AndroidRuntime(753): at android.view.View.draw(View.java:6535)
码
public void clearBitmap() {
try{
if(bitmap!=null && !bitmap.isRecycled()){
bitmap.recycle();
bitmap = null;
}
}catch(Throwable e){
System.out.println(e.getMessage());
e.printStackTrace();
}
System.gc();
}
答案 0 :(得分:0)
让垃圾收集器处理它。在您的情况下或在任何正常情况下,您不需要使用Bitmap#recycle()
。