Reputation: 4650
I am working in an android application and I want to Clear my Bitmap data. The scenario is that I am taking a screen capture of an Widget(Imageview) and I am storing it in a Bitmap. This action comes in a Button click. SO after some time I get a Memory error. So I want to clear the values in the bitmap. So to do that I have done the following code : The BitMap variable is mCaptureImageBitmap
public void ButtonClick(View v)
{
mCaptureImageBitmap.recycle();
mCaptureImageBitmap=null;
View ve = findViewById(R.id.mainscreenGlViewRelativeLayout);
ve.setDrawingCacheEnabled(true);
mCaptureImageBitmap = ve.getDrawingCache();
}
But I get an error of NullPoint exception. Please help me
Upvotes: 1
Views: 5281
Reputation: 4821
Try this code ...
ve.setDrawingCacheEnabled(true);
// Add these lines
ve.measure(MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED), MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED));
ve.layout(0, 0, ve.getMeasuredWidth(), ve.getMeasuredHeight());
ve.buildDrawingCache(true);
Bitmap b = Bitmap.createBitmap(ve.getDrawingCache());
mCaptureImageBitmap = b;
ve.setDrawingCacheEnabled(false); // clear drawing cache
Upvotes: 1
Reputation: 4397
You have most of the right code but in the wrong order. Try doing something like this
public void ButtonClick(View v)
{
Bitmap mCaptureImageBitmap;
final View ve = findViewById(R.id.mainscreenGlViewRelativeLayout);
ve.setDrawingCacheEnabled(true);
mCaptureImageBitmap = ve.getDrawingCache();
// Do something useful with your image here
mCaptureImageBitmap.recycle();
mCaptureImageBitmap = null;
}
Upvotes: 1