我创建了一个Button来捕获Webview的屏幕截图并将其保存到DCIM中的文件夹中。问题是当我点击按钮时,屏幕截图被捕获并保存在图库中,但当我再次点击时,旧屏幕截图再次以新名称保存。
这是我的代码。请帮忙
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);
}
}
答案 0 :(得分:0)
根据您的代码,您为每次点击生成随机数,并使用包含随机数的文件名保存它们吗?这就是为什么你要获得具有不同名称的多个图像...你可能想重新考虑文件名生成代码来解决这个问题..
您可能想要删除此代码:
`idTransactions`, `idMemberCard`, `idAccount`
131534, 64925, 292
131535, 64925, 292
131551, 64925, 291
131539, 64926 292
131540, 64926, 292
131541, 64926, 292
131550, 64926, 291
131544, 64928, 291
131545, 64928, 291
131564, 64928, 293
131566, 64928, 293
131567, 64929, 293
131568, 64929, 293
131546, 64929, 291
131547, 64929, 291
131548, 64929, 291
131549, 64929, 291
并删除i1变量:
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 imagePath = new File(Environment.getExternalStorageDirectory() + "/DCIM/NCERT Stuff/NCERT"+i1+sb+".jpeg");
答案 1 :(得分:0)
我认为旧位图仍然是您的绘图缓存。请尝试链接中的方法。
答案 2 :(得分:-1)