friend 们大家好,我正在使用 webview 制作一个应用程序 我想截取我的 Activity 截图 目前我正在使用此代码来捕获图像
public Bitmap takeScreenshot() {
View rootView = findViewById(android.R.id.content).getRootView();
rootView.setDrawingCacheEnabled(true);
return rootView.getDrawingCache();
}
这个要保存
public void saveBitmap(Bitmap bitmap) {
File imagePath = new File(Environment.getExternalStorageDirectory() + "/screenshot.png");
FileOutputStream fos;
try {
fos = new FileOutputStream(imagePath);
bitmap.compress(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);
}
}
它有时有效,有时无效,我的意思是,有时我可以在图库屏幕截图中看到,有时则不行 我想是否有任何选项可以显示捕获的屏幕截图 就像我们按下音量+电源按钮
主要问题是如何在拍摄时显示图像 或知道它是否被采取 提前致谢
最佳答案
这是一个示例方法:保存图像 -> 通过对话框显示结果。为了使其在 Android Gallery 中可用,还应该更改文件路径:
public void saveBitmap(Bitmap bitmap) {
File path = Environment.getExternalStoragePublicDirectory(
Environment.DIRECTORY_PICTURES);
File imagePath = new File(path, "screenshot.png");//now gallery can see it
FileOutputStream fos;
try {
fos = new FileOutputStream(imagePath);
bitmap.compress(CompressFormat.JPEG, 100, fos);
fos.flush();
fos.close();
displayResult(imagePath.getAbsolutePath())// here you display your result after saving
} catch (FileNotFoundException e) {
Log.e("GREC", e.getMessage(), e);
} catch (IOException e) {
Log.e("GREC", e.getMessage(), e);
}
}
创建另一个方法调用displayResult
来查看结果:
public void displayResult(String imagePath){
//LinearLayOut Setup
LinearLayout linearLayout= new LinearLayout(this);
linearLayout.setOrientation(LinearLayout.VERTICAL);
linearLayout.setLayoutParams(new LayoutParams(
LayoutParams.MATCH_PARENT,
LayoutParams.MATCH_PARENT));
//ImageView Setup
ImageView imageView = new ImageView(this);//you need control the context of Imageview
//Diaglog setup
final Dialog dialog = new Dialog(this);//you need control the context of this dialog
dialog.setContentView(imageView);
imageView.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
dialog.dismiss();
}
});
//Display result
imageView.setImageBitmap(BitmapFactory.decodeFile(imagePath));
dialog.show();
}
关于android Activity 截图如何?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27911894/