如何以编程方式在警报对话框的 android 中截取屏幕截图
How To Programmatically take a screenshot in android of Alert Dialog
我看过以下内容 Link 并且确实截取了最佳答案的屏幕截图
但是,我想要的是让应用截取我向用户显示的警报对话框的屏幕截图,上面的解决方案和下面的代码只截取当前在警报对话框后面的内容的屏幕截图,因此不好
如果有人没有通过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();
}
进一步编辑:
这里你会看到两个截图,第一个是保存的截图,当所有内容都保存在对话框的截图中时,你会注意到底部有一些文本总是出现在底部.
第二个屏幕截图是对话框中文本太多的地方,对话框是可滚动的,因此您可以看到所有数据,您会注意到第一个屏幕截图中底部的字符串不存在
如果可能的话,我希望显示所有数据,但我不确定屏幕截图功能是否能够做到这一点或其他方法
试试这个库:
https://github.com/jraska/Falcon
它可以将对话捕获到您的屏幕截图中。
1.亲爱的朋友,你做错了一件事,你无法截取对话框。
View v1 = getWindow().getDecorView().getRootView();
您正在捕获 AlertDialog 下方的整个屏幕
您可以使用这些方法通过将对话框的视图发送到此方法来完成您的工作
Get Bitmap From A view
public static Bitmap loadView(View v) {
Bitmap b = Bitmap.createBitmap(width , height, Bitmap.Config.ARGB_8888);
Canvas c = new Canvas(b);
v.layout(0, 0, v.getWidth(), v.getHeight());
v.draw(c);
return b;
}
Saving Your Bitmap
void saveFile(Bitmap bitmap) {
String extr = Environment.getExternalStorageDirectory().toString() + File.separator + "Folder";
String fileName = new SimpleDateFormat("yyyyMMddhhmm'_bitmap.jpg'", Locale.US).format(new Date());
File myPath = new File(extr, fileName);
FileOutputStream fos = null;
try {
fos = new FileOutputStream(myPath);
bitmap.compress(Bitmap.CompressFormat.JPEG, 100, fos);
fos.flush();
fos.close();
MediaStore.Images.Media.insertImage(getContentResolver(), bitmap, "Screen", "screen");
} catch (Exception e) {
e.printStackTrace();
}
}
在 Android 5 模拟器及其工作上开发。从您提供的 link 中获取您的对话代码和屏幕截图代码。
这是你的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().getDecorView().getRootView();
移动到对话代码,则可以概括方法 takeScreenshot
调用此方法的位置。
注2:看到你更新了问题。当其中一些被隐藏时,我认为您无法在屏幕截图中获取全部数据。把它想象成一个普通的截图(在电脑上,甚至 phone)。你只拍你能看到的东西。
我看过以下内容 Link 并且确实截取了最佳答案的屏幕截图
但是,我想要的是让应用截取我向用户显示的警报对话框的屏幕截图,上面的解决方案和下面的代码只截取当前在警报对话框后面的内容的屏幕截图,因此不好
如果有人没有通过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();
}
进一步编辑:
这里你会看到两个截图,第一个是保存的截图,当所有内容都保存在对话框的截图中时,你会注意到底部有一些文本总是出现在底部.
第二个屏幕截图是对话框中文本太多的地方,对话框是可滚动的,因此您可以看到所有数据,您会注意到第一个屏幕截图中底部的字符串不存在
如果可能的话,我希望显示所有数据,但我不确定屏幕截图功能是否能够做到这一点或其他方法
试试这个库:
https://github.com/jraska/Falcon
它可以将对话捕获到您的屏幕截图中。
1.亲爱的朋友,你做错了一件事,你无法截取对话框。
View v1 = getWindow().getDecorView().getRootView();
您正在捕获 AlertDialog 下方的整个屏幕
您可以使用这些方法通过将对话框的视图发送到此方法来完成您的工作
Get Bitmap From A view
public static Bitmap loadView(View v) {
Bitmap b = Bitmap.createBitmap(width , height, Bitmap.Config.ARGB_8888);
Canvas c = new Canvas(b);
v.layout(0, 0, v.getWidth(), v.getHeight());
v.draw(c);
return b;
}
Saving Your Bitmap
void saveFile(Bitmap bitmap) {
String extr = Environment.getExternalStorageDirectory().toString() + File.separator + "Folder";
String fileName = new SimpleDateFormat("yyyyMMddhhmm'_bitmap.jpg'", Locale.US).format(new Date());
File myPath = new File(extr, fileName);
FileOutputStream fos = null;
try {
fos = new FileOutputStream(myPath);
bitmap.compress(Bitmap.CompressFormat.JPEG, 100, fos);
fos.flush();
fos.close();
MediaStore.Images.Media.insertImage(getContentResolver(), bitmap, "Screen", "screen");
} catch (Exception e) {
e.printStackTrace();
}
}
在 Android 5 模拟器及其工作上开发。从您提供的 link 中获取您的对话代码和屏幕截图代码。
这是你的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().getDecorView().getRootView();
移动到对话代码,则可以概括方法 takeScreenshot
调用此方法的位置。
注2:看到你更新了问题。当其中一些被隐藏时,我认为您无法在屏幕截图中获取全部数据。把它想象成一个普通的截图(在电脑上,甚至 phone)。你只拍你能看到的东西。