【问题标题】:android activity screenshot how?android活动截图怎么样?
【发布时间】:2015-01-15 04:27:21
【问题描述】:

你好朋友我正在用 webview 制作一个应用程序 我想截取我的活动截图 目前我正在使用此代码来捕获图像

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 screenshot


【解决方案1】:

这是一个示例方法:保存图像 -> 使用对话框显示结果。为了使其在 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();

}

【讨论】:

  • 感谢 Nguyen Doan Tung;;使用您的方法;;但我在画廊中看不到截图;;有没有办法在画廊截图中看到;;捕获
  • 谢谢老兄,它现在可以工作了;我使用了 sendbroadcast(intent) 方法它的工作
  • @user3565993 你能解释一下你是如何使用 sendboardcast(intent)
【解决方案2】:

其他人在尝试从视图的绘图缓存中获取位图时遇到了问题。

您可以直接从视图中获取位图,而不是尝试使用绘图缓存。 Google 的 DynamicListView 示例使用以下函数来执行此操作。

 /** Returns a bitmap showing a screenshot of the view passed in. */
private Bitmap getBitmapFromView(View v) {
    Bitmap bitmap = Bitmap.createBitmap(v.getWidth(), v.getHeight(), Bitmap.Config.ARGB_8888);
    Canvas canvas = new Canvas (bitmap);
    v.draw(canvas);
    return bitmap;
}

【讨论】:

  • 你好戴夫 S;我怎么能调用这个函数?位图 bitmap= getBitmapFromView(?what?);
  • "return getBitmapFromView(rootView)" 在 takeScreenshot() 的最后一行
  • 你好-斯蒂芬;;我以这种方式尝试过;;公共`位图 takeScreenshot() { 查看 rootView = findViewById(android.R.id.content).getRootView();返回返回 getBitmapFromView(rootView); }` 什么也没发生;我认为屏幕截图没有保存
  • 好吧,我可以向您保证该功能有效,我现在正在我的应用程序中使用它。这可能是您如何保存或验证屏幕截图的问题。你能用更多代码编辑你的答案并展示你是如何尝试使用它的吗?
  • 谢谢 Dave,用你的方法和 Nguyen duan Tung 方法我可以截图了 ;;但 ;;;它在文件管理器中可见,而不是在图库中
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-06-10
  • 1970-01-01
  • 2013-11-17
  • 1970-01-01
  • 1970-01-01
  • 2012-11-06
相关资源
最近更新 更多