我有一个RelativeLayout视图,其中包括一些TextView和一个地图片段。我想截取整个屏幕的屏幕截图(包括显示的地图和文本)并保存它。一切正常,但地图视图显示黑色图像。这怎么可能?
我还尝试了一些延迟间隔,以确保地图已完全加载,但没有工作。
Bitmap bitmap = takeScreenshot();
createDirectoryAndSaveFile(bitmap,intent.getStringExtra("TEST"));
public Bitmap takeScreenshot() {
View rootView = findViewById(R.id.relative_layout);
rootView.setDrawingCacheEnabled(true);
return rootView.getDrawingCache();
}
private void createDirectoryAndSaveFile(Bitmap bitmap, String name) {
File folder = new File(Environment.getExternalStorageDirectory() +"/Test");
if (!folder.exists()) {
File screenshoDir = new File(Environment.getExternalStorageDirectory() + "/Test/");
screenshoDir.mkdirs();
}
File file = new File(new File(Environment.getExternalStorageDirectory() + "/Test/"), name);
if (file.exists()) {
file.delete();
}
try {
FileOutputStream out = new FileOutputStream(file);
bitmap.compress(Bitmap.CompressFormat.JPEG, 90, out);
out.flush();
out.close();
MediaScannerConnection.scanFile(this, new String[]{file.getPath()}, new String[]{"image/jpeg"}, null);
} catch (Exception e) {
e.printStackTrace();
}
}