我看过以下 Link并且它确实截取了最佳答案的屏幕截图
但是,我想要的是让应用程序截取我向用户显示的警报对话框的屏幕截图,上面的解决方案和下面的代码只截取了当前在警报对话框后面的内容的屏幕截图,因此不好
如果有人没有通过提供的链接,这里是使用的代码
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();
}
进一步编辑:
在这里您将看到两个屏幕截图,第一个是显示已保存的屏幕截图,当所有内容都保存在对话框的屏幕截图中时,您会注意到在底部有一些文本始终出现在底部。
第二个屏幕截图是对话框中有太多文本的地方,对话框是可滚动的,因此您可以看到所有数据,您会注意到第一个屏幕截图中底部的字符串不存在
如果可能的话,我希望显示所有数据,但我不确定屏幕截图功能是否能够做到这一点或其他方法
最佳答案
在 Android 5 模拟器及其工作上开发。从您提供的链接中获取您的对话代码和屏幕截图代码。
这是您的AlertDialog
public void showCalc(String title, String message) {
final 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) {
AlertDialog dialog2 =AlertDialog.class.cast(dialog);
takeScreenshot(dialog2);
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();
}
这是截图代码
private void takeScreenshot(AlertDialog 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 = "/data/data/com.rohit.test/test.jpg"; // use your desired path
// create bitmap screen capture
View v1 = dialog.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();
} catch (Throwable e) {
// Several error may come out with file handling or OOM
e.printStackTrace();
}
}
截图
注意 1: 如果将参数类型更改为 View
并移动 dialog.getWindow() ,则可以概括方法
到调用此方法的对话代码。takeScreenshot
.getDecorView().getRootView();
注意 2: 看到您更新了问题。当其中一些被隐藏时,我认为您无法在屏幕截图中获取全部数据。把它想象成一个普通的屏幕截图(在电脑甚至手机上)。你只拍下你能看到的东西。
关于android - 如何以编程方式在警报对话框的 android 中截取屏幕截图,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35430817/