我看到了以下链接,它可以拍摄带有顶部答案的截图。
然而,我想让应用程序拍摄我向用户显示的Alert Dialog的截图,上述解决方案和下面的代码只会拍摄当前Alert Dialog后面的内容,因此不好用。
如果有人还没有阅读提供的链接,请查看以下正在使用的代码。
然而,我想让应用程序拍摄我向用户显示的Alert Dialog的截图,上述解决方案和下面的代码只会拍摄当前Alert Dialog后面的内容,因此不好用。
如果有人还没有阅读提供的链接,请查看以下正在使用的代码。
Date now = new Date();
android.text.format.DateFormat.format("yyyy-MM-dd_hh:mm:ss", now);
try {
// image naming and path to include sd card appending name you choose for file
String mPath = Environment.getExternalStorageDirectory().toString() + "/" + now + ".jpg";
// create bitmap screen capture
View v1 = getWindow().getDecorView().getRootView();
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();
openScreenshot(imageFile);
} catch (Throwable e) {
// Several error may come out with file handling or OOM
e.printStackTrace();
}
编辑:根据要求提供对话框的代码
public void showCalc(String title, String message) {
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setCancelable(true);
builder.setTitle(title);
builder.setMessage(message);
builder.setPositiveButton("Capture + Open",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
//Remove Values From Inventory
captureScreenAndOpen();
}
});
builder.setNegativeButton("Capture",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
captureScreen();
Context context = getApplicationContext();
Toast.makeText(context, "Screenshot Captured", Toast.LENGTH_LONG).show();
}
});
builder.setNeutralButton("Return", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
}
});
builder.show();
}
进一步编辑:
这里你将看到两张截图,第一张展示了当从对话框中保存所有内容的截图时,你会注意到底部总是有一些文本。
第二张屏幕截图是当对话框中有大量文本时,对话框是可滚动的,这样您就可以查看所有数据,您会注意到第一个屏幕截图中的底部字符串不存在。 如果可能的话,我希望所有的数据都能显示出来。但我不确定是截屏功能可以做到这一点,还是需要其他方法。