android Activity 截图如何?

标签 android screenshot

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/

相关文章:

android - 如何更改 timepickerdialog 中“确定”和“取消”按钮布局的背景颜色

C# CopyFromScreen 问题

ios - 截图最顶层/ View

java - 我想把 AsyncTask 放到外面。在任务数据中

android - 在 ViewPager 的 fragment 内滑动 ListView 47deg

android - 从GCM迁移到FCM后的PeriodicTask和OneoffTask

python - 如何使用Python选择屏幕上的区域并使未选择的区域变暗?

java - 控制Android上的WIFI\蜂窝数据更新频率

ios - 没有截图的本地化?

android - 截图(root)