简体   繁体   English

如何以编程方式保存屏幕截图而不会覆盖之前拍摄的屏幕截图?

[英]How can I save a screenshots programmatically with out over writing the previously taken screenshot?

I'm working on an android application and I want to save a screenshot of the application.我正在开发一个 android 应用程序,我想保存该应用程序的屏幕截图。 I can save a single screenshot but it keeps over writing the previous screenshot.我可以保存一个屏幕截图,但它会继续写上一个屏幕截图。 I followed a tutorial and modified it but it does not take more than a single screenshot我按照教程进行了修改,但只需要一个屏幕截图

Attached here is the code in the button action这里附上按钮动作中的代码

      case R.id.btn_save:
            View rootView = getWindow().getDecorView().findViewById(android.R.id.content);
            Bitmap bitmap = getScreenShot(rootView);
            int i = 0;
            File file = new File("ScreenShot"+ i +".PNG");
            if(!file.exists()){
                store(bitmap, "ScreenShot"+ i +".PNG");
            }
            else {
                store(bitmap, "ScreenShot"+ i++ +".PNG");
            }

and the screenshot storing function以及存储 function 的屏幕截图

public void store(Bitmap bm, String fileName){
    String dirPath = Environment.getExternalStorageDirectory().getAbsolutePath() + "/Screenshots";
    File dir = new File(dirPath);
    if (!dir.exists()){
        dir.mkdirs();
    }
    File file = new File(dirPath,fileName);
    try{
        FileOutputStream fos = new FileOutputStream(file);
        bm.compress(Bitmap.CompressFormat.PNG, 100,fos);
        fos.flush();
        fos.close();
    }catch (Exception e){
        e.printStackTrace();
        Toast.makeText(this, "Error saving File", Toast.LENGTH_SHORT).show();
    }
}

You are declaring i variable inside the button save so you will always start with 0 when button is clicked.您在按钮保存中声明 i 变量,因此单击按钮时您将始终从 0 开始。 To use the way you are trying you should declare that variable outside that scope, but it will restart when you kill and reopen the app.要使用您尝试的方式,您应该在 scope 之外声明该变量,但是当您杀死并重新打开应用程序时它将重新启动。

You can use Shared Preferences to save the following number to use (or the last you used) if you want to use that approach.如果您想使用该方法,您可以使用 Shared Preferences 保存以下数字以供使用(或最后一次使用)。 If not you can use simply如果没有,您可以简单地使用

"Screenshot" + System.currentTimeInMillis().toString(). 

You will also have the time when the screenshot was taken (although in millis).您还将获得截取屏幕截图的时间(尽管以毫秒为单位)。 If you want you can format it to be like "user readable" 20191110 for example如果您愿意,可以将其格式化为“用户可读” 20191110 例如

Because in that code the file name is always the same- i is always 0. To make it work for one use of the app, i should be a member variable and incremented every screenshot.因为在该代码中,文件名始终相同 - i 始终为 0。为了使其适用于应用程序的一次使用,i 应该是一个成员变量,并在每个屏幕截图中递增。 To make it work more generally, you should generate a random name using File.createTempFile()为了使其更普遍地工作,您应该使用 File.createTempFile() 生成一个随机名称

case R.id.btn_save:
   View rootView getWindow().getDecorView().findViewById(android.R.id.content);
   Bitmap bitmap = getScreenShot(rootView);

   File dir = new File(Environment.getExternalStorageDirectory(), "Screenshots");

   if (!dir.exists())
    if ( !dir.mkdirs())
        {
          Toast ( could not create dir...);
          return;
        }

 int i = 0;
 while (++i > 0 )
  {
  String fileName = "ScreenShot"+ i +".png";

  File file = new File(dir, fileName);
  if(!file.exists())
       {
         store(bitmap, file);

         break;
        }
  }
  break;     

Change the parameter of store(Bitmap bm, String fileName) to store(Bitmap bm, File file)store(Bitmap bm, String fileName)的参数改为store(Bitmap bm, File file)

There you can remove all code before the try block.在那里,您可以在 try 块之前删除所有代码。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM