I have created a Button to capture screenshot of the Webview and save it to a Folder in DCIM. The Problem is when I click on the Button, the screenshot gets captured and saves in the Gallery but when I click again the Old Screenshot saves again with a New name.
Here is my Code. Please help
screenshot.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Bitmap bitmap = takeScreenshot();
saveBitmap(bitmap);
}
});
public Bitmap takeScreenshot() {
mWebview.setDrawingCacheEnabled(true);
return mWebview.getDrawingCache();
}
public void saveBitmap(Bitmap bitmap) {
Random r = new Random();
long i1 = r.nextInt(9999-9) + 9;
char[] chars = "ABCDEF".toCharArray();
StringBuilder sb = new StringBuilder();
Random random = new Random();
for (int i = 0; i < 20; i++) {
char c = chars[random.nextInt(chars.length)];
sb.append(c);
}
File directory = new File(Environment.getExternalStorageDirectory() + "/DCIM/NCERT Stuff/");
if(!directory.exists()) {
directory.mkdirs();
}
File imagePath = new File(Environment.getExternalStorageDirectory() + "/DCIM/NCERT Stuff/NCERT"+i1+sb+".jpeg");
// sendBroadcast(new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE, Uri.fromFile(imagePath)));
FileOutputStream fos;
try {
fos = new FileOutputStream(imagePath);
bitmap.compress(Bitmap.CompressFormat.JPEG, 100, fos);
fos.flush();
fos.close();
} catch (FileNotFoundException e) {
Log.e("GREC", e.getMessage(), e);
} catch (IOException e) {
Log.e("GREC", e.getMessage(), e);
}
}