I tried your code with minor changes by shifting position of stream and adding bitmap.recycle() followed by assigning it to null and I couldn't find any memory leak with it.
Your screen capture code is working fine...
I am attaching the code snippet I tried in android studio for your reference. Also attached memory log
Please check what's happening after capturing snap
package com.fun.test;
import android.content.Context;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.os.Environment;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Toast;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Date;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void capture(View view) {
View v1 = getWindow().getDecorView().getRootView();
File imagefile = new File(Environment.getExternalStorageDirectory().getPath()+ "/screenshot.png");
try {
imagefile.createNewFile();
} catch (IOException e) {
e.printStackTrace();
}
takeScreenshot(v1, imagefile, this);
}
public void takeScreenshot(View v1, File f, Context context) {
Date now = new Date();
android.text.format.DateFormat.format("yyyy-MM-dd_hh:mm:ss", now);
try {
String mPath = f.getPath();
// create bitmap screen capture
v1.setDrawingCacheEnabled(true);
Bitmap bitmap = Bitmap.createBitmap(v1.getDrawingCache());
v1.setDrawingCacheEnabled(false);
File imageFile = new File(mPath);
FileOutputStream outputStream = new FileOutputStream(imageFile);
int quality = 100;
bitmap.compress(Bitmap.CompressFormat.JPEG, quality, outputStream);
outputStream.flush();
outputStream.close();
//trying to release memory
/*Canvas bitmapCanvas = new Canvas(bitmap);
bitmap.eraseColor(Color.TRANSPARENT);
bitmapCanvas.setBitmap(null);
bitmapCanvas = null;*/
bitmap.recycle();
bitmap = null;
} catch (Throwable e) {
// Several error may come out with file handling or OOM
e.printStackTrace();
Toast.makeText(context, "An error occurred!" + e.getMessage().toString() , Toast.LENGTH_LONG).show();
}
}
}
And this is the memory allocation log from android studio
10-09 19:08:17.195 28618-28618/com.fun.test D/GestureDetector: [Surface Touch Event] mSweepDown False, mLRSDCnt : -1 mTouchCnt : 6 mFalseSizeCnt:0
10-09 19:08:17.275 28618-28618/com.fun.test D/dalvikvm: GC_FOR_ALLOC freed 245K, 23% free 7702K/9884K, paused 62ms, total 63ms
10-09 19:08:17.285 28618-28618/com.fun.test I/dalvikvm-heap: Grow heap (frag case) to 11.386MB for 2073616-byte allocation
10-09 19:08:17.295 28618-28627/com.fun.test D/dalvikvm: GC_FOR_ALLOC freed 5K, 19% free 9722K/11912K, paused 16ms, total 16ms
10-09 19:08:17.335 28618-28622/com.fun.test E/dalvikvm: adjustAdaptiveCoef max=4194304, min=1048576, ut=568
10-09 19:08:17.335 28618-28622/com.fun.test D/dalvikvm: GC_CONCURRENT freed <1K, 19% free 9722K/11912K, paused 2ms+16ms, total 38ms
10-09 19:08:17.335 28618-28618/com.fun.test D/dalvikvm: WAIT_FOR_CONCURRENT_GC blocked 27ms
10-09 19:08:17.345 28618-28618/com.fun.test I/dalvikvm-heap: Grow heap (frag case) to 13.359MB for 2073616-byte allocation
10-09 19:08:17.355 28618-28627/com.fun.test E/dalvikvm: adjustAdaptiveCoef max=6291456, min=1572864, ut=368
10-09 19:08:17.365 28618-28627/com.fun.test D/dalvikvm: GC_FOR_ALLOC freed <1K, 16% free 11747K/13940K, paused 26ms, total 26ms
10-09 19:08:37.085 28618-28618/com.fun.test D/GestureDetector: [Surface Touch Event] mSweepDown False, mLRSDCnt : -1 mTouchCnt : 3 mFalseSizeCnt:0
10-09 19:08:37.105 28618-28618/com.fun.test D/dalvikvm: GC_FOR_ALLOC freed 8126K, 23% free 7698K/9884K, paused 19ms, total 19ms
10-09 19:08:37.115 28618-28618/com.fun.test I/dalvikvm-heap: Grow heap (frag case) to 11.382MB for 2073616-byte allocation
10-09 19:08:37.125 28618-28627/com.fun.test D/dalvikvm: GC_FOR_ALLOC freed <1K, 19% free 9723K/11912K, paused 14ms, total 14ms
10-09 19:08:37.145 28618-28622/com.fun.test E/dalvikvm: adjustAdaptiveCoef max=4194304, min=1048576, ut=568
10-09 19:08:37.145 28618-28622/com.fun.test D/dalvikvm: GC_CONCURRENT freed <1K, 19% free 9723K/11912K, paused 3ms+2ms, total 20ms
10-09 19:08:37.145 28618-28618/com.fun.test D/dalvikvm: WAIT_FOR_CONCURRENT_GC blocked 10ms
10-09 19:08:37.145 28618-28618/com.fun.test I/dalvikvm-heap: Grow heap (frag case) to 13.360MB for 2073616-byte allocation
10-09 19:08:37.165 28618-28627/com.fun.test E/dalvikvm: adjustAdaptiveCoef max=6291456, min=1572864, ut=368
10-09 19:08:37.165 28618-28627/com.fun.test D/dalvikvm: GC_FOR_ALLOC freed <1K, 16% free 11748K/13940K, paused 12ms, total 12ms
10-09 19:08:51.208 28618-28618/com.fun.test D/GestureDetector: [Surface Touch Event] mSweepDown False, mLRSDCnt : -1 mTouchCnt : 3 mFalseSizeCnt:0
10-09 19:08:51.228 28618-28618/com.fun.test D/dalvikvm: GC_FOR_ALLOC freed 8124K, 23% free 7698K/9884K, paused 20ms, total 20ms
10-09 19:08:51.238 28618-28618/com.fun.test I/dalvikvm-heap: Grow heap (frag case) to 11.382MB for 2073616-byte allocation
10-09 19:08:51.248 28618-28627/com.fun.test D/dalvikvm: GC_FOR_ALLOC freed <1K, 19% free 9723K/11912K, paused 15ms, total 15ms
10-09 19:08:51.268 28618-28622/com.fun.test E/dalvikvm: adjustAdaptiveCoef max=4194304, min=1048576, ut=568
10-09 19:08:51.268 28618-28622/com.fun.test D/dalvikvm: GC_CONCURRENT freed <1K, 19% free 9723K/11912K, paused 2ms+1ms, total 19ms
10-09 19:08:51.268 28618-28618/com.fun.test D/dalvikvm: WAIT_FOR_CONCURRENT_GC blocked 11ms
10-09 19:08:51.268 28618-28618/com.fun.test I/dalvikvm-heap: Grow heap (frag case) to 13.360MB for 2073616-byte allocation
10-09 19:08:51.288 28618-28618/com.fun.test E/dalvikvm: adjustAdaptiveCoef max=6291456, min=1572864, ut=368
10-09 19:08:51.288 28618-28618/com.fun.test D/dalvikvm: GC_FOR_ALLOC freed <1K, 16% free 11748K/13940K, paused 13ms, total 13ms
10-09 19:08:58.676 28618-28618/com.fun.test D/GestureDetector: [Surface Touch Event] mSweepDown False, mLRSDCnt : -1 mTouchCnt : 4 mFalseSizeCnt:0
10-09 19:08:59.516 28618-28618/com.fun.test D/GestureDetector: [Surface Touch Event] mSweepDown False, mLRSDCnt : -1 mTouchCnt : 2 mFalseSizeCnt:0
10-09 19:08:59.546 28618-28618/com.fun.test D/dalvikvm: GC_FOR_ALLOC freed 8123K, 23% free 7698K/9884K, paused 19ms, total 20ms
10-09 19:08:59.546 28618-28618/com.fun.test I/dalvikvm-heap: Grow heap (frag case) to 11.382MB for 2073616-byte allocation
10-09 19:08:59.566 28618-28627/com.fun.test D/dalvikvm: GC_FOR_ALLOC freed <1K, 19% free 9723K/11912K, paused 15ms, total 15ms
10-09 19:08:59.586 28618-28622/com.fun.test E/dalvikvm: adjustAdaptiveCoef max=4194304, min=1048576, ut=568
10-09 19:08:59.586 28618-28622/com.fun.test D/dalvikvm: GC_CONCURRENT freed <1K, 19% free 9723K/11912K, paused 3ms+2ms, total 19ms
10-09 19:08:59.586 28618-28618/com.fun.test D/dalvikvm: WAIT_FOR_CONCURRENT_GC blocked 10ms
10-09 19:08:59.586 28618-28618/com.fun.test I/dalvikvm-heap: Grow heap (frag case) to 13.360MB for 2073616-byte allocation
10-09 19:08:59.606 28618-28627/com.fun.test E/dalvikvm: adjustAdaptiveCoef max=6291456, min=1572864, ut=368
10-09 19:08:59.606 28618-28627/com.fun.test D/dalvikvm: GC_FOR_ALLOC freed <1K, 16% free 11748K/13940K, paused 16ms, total 16ms
10-09 19:09:08.085 28618-28618/com.fun.test D/GestureDetector: [Surface Touch Event] mSweepDown False, mLRSDCnt : -1 mTouchCnt : 5 mFalseSizeCnt:0
10-09 19:09:09.066 28618-28618/com.fun.test D/GestureDetector: [Surface Touch Event] mSweepDown False, mLRSDCnt : -1 mTouchCnt : 3 mFalseSizeCnt:0
10-09 19:09:09.096 28618-28618/com.fun.test D/dalvikvm: GC_FOR_ALLOC freed 8123K, 23% free 7698K/9884K, paused 18ms, total 18ms
10-09 19:09:09.096 28618-28618/com.fun.test I/dalvikvm-heap: Grow heap (frag case) to 11.382MB for 2073616-byte allocation
10-09 19:09:09.116 28618-28627/com.fun.test D/dalvikvm: GC_FOR_ALLOC freed <1K, 19% free 9723K/11912K, paused 15ms, total 15ms
10-09 19:09:09.136 28618-28622/com.fun.test E/dalvikvm: adjustAdaptiveCoef max=4194304, min=1048576, ut=568
10-09 19:09:09.136 28618-28622/com.fun.test D/dalvikvm: GC_CONCURRENT freed <1K, 19% free 9723K/11912K, paused 3ms+1ms, total 20ms
10-09 19:09:09.136 28618-28618/com.fun.test D/dalvikvm: WAIT_FOR_CONCURRENT_GC blocked 11ms
10-09 19:09:09.136 28618-28618/com.fun.test I/dalvikvm-heap: Grow heap (frag case) to 13.360MB for 2073616-byte allocation
10-09 19:09:09.156 28618-28627/com.fun.test E/dalvikvm: adjustAdaptiveCoef max=6291456, min=1572864, ut=368
10-09 19:09:09.156 28618-28627/com.fun.test D/dalvikvm: GC_FOR_ALLOC freed <1K, 16% free 11748K/13940K, paused 15ms, total 15ms
10-09 19:09:16.163 28618-28618/com.fun.test D/GestureDetector: [Surface Touch Event] mSweepDown False, mLRSDCnt : -1 mTouchCnt : 2 mFalseSizeCnt:0
10-09 19:09:18.605 28618-28618/com.fun.test D/GestureDetector: [Surface Touch Event] mSweepDown False, mLRSDCnt : -1 mTouchCnt : 4 mFalseSizeCnt:0
10-09 19:09:18.645 28618-28618/com.fun.test D/dalvikvm: GC_FOR_ALLOC freed 8123K, 23% free 7698K/9884K, paused 31ms, total 31ms
10-09 19:09:18.645 28618-28618/com.fun.test I/dalvikvm-heap: Grow heap (frag case) to 11.383MB for 2073616-byte allocation
10-09 19:09:18.655 28618-28627/com.fun.test D/dalvikvm: GC_FOR_ALLOC freed <1K, 19% free 9723K/11912K, paused 15ms, total 15ms
10-09 19:09:18.685 28618-28622/com.fun.test E/dalvikvm: adjustAdaptiveCoef max=4194304, min=1048576, ut=568
10-09 19:09:18.685 28618-28622/com.fun.test D/dalvikvm: GC_CONCURRENT freed <1K, 19% free 9723K/11912K, paused 3ms+1ms, total 21ms
10-09 19:09:18.685 28618-28618/com.fun.test D/dalvikvm: WAIT_FOR_CONCURRENT_GC blocked 12ms
10-09 19:09:18.685 28618-28618/com.fun.test I/dalvikvm-heap: Grow heap (frag case) to 13.360MB for 2073616-byte allocation
10-09 19:09:18.695 28618-28627/com.fun.test E/dalvikvm: adjustAdaptiveCoef max=6291456, min=1572864, ut=368
10-09 19:09:18.695 28618-28627/com.fun.test D/dalvikvm: GC_FOR_ALLOC freed <1K, 16% free 11748K/13940K, paused 14ms, total 14ms