Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/198.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何以编程方式保存屏幕截图,而不过度编写以前拍摄的屏幕截图?_Java_Android_Android Bitmap - Fatal编程技术网

Java 如何以编程方式保存屏幕截图,而不过度编写以前拍摄的屏幕截图?

Java 如何以编程方式保存屏幕截图,而不过度编写以前拍摄的屏幕截图?,java,android,android-bitmap,Java,Android,Android Bitmap,我正在开发一个android应用程序,我想保存该应用程序的屏幕截图。我可以保存一个屏幕截图,但它会继续写上一个屏幕截图。 我按照教程修改了它,但只需要一个屏幕截图 附件是按钮操作中的代码 case R.id.btn_save: View rootView = getWindow().getDecorView().findViewById(android.R.id.content); Bitmap bitmap = getScreenSh

我正在开发一个android应用程序,我想保存该应用程序的屏幕截图。我可以保存一个屏幕截图,但它会继续写上一个屏幕截图。 我按照教程修改了它,但只需要一个屏幕截图

附件是按钮操作中的代码

      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");
            }
以及屏幕截图存储功能

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();
    }
}

因为在该代码中,文件名始终相同-i始终为0。为了让应用程序只使用一次,我应该是一个成员变量,并增加每个屏幕截图。为了使其更普遍地工作,您应该使用File.createTempFile()生成一个随机名称。

您在按钮保存中声明了i变量,因此当单击按钮时,您总是从0开始。要使用您尝试的方式,您应该在该范围之外声明该变量,但当您终止并重新打开应用程序时,它将重新启动

如果要使用该方法,可以使用共享首选项保存以下数字(或上次使用的数字)。如果没有,你可以简单地使用

"Screenshot" + System.currentTimeInMillis().toString(). 
您还将有截图拍摄的时间(尽管以毫秒为单位)。例如,如果需要,可以将其格式化为“用户可读”20191110

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;     
store(位图bm,字符串文件名)
的参数更改为
store(位图bm,文件文件名)


在那里,您可以删除try块之前的所有代码。

屏幕截图存储为什么?它最终创建了什么文件名,并且现在一直覆盖它。ScreenShot0.jpg?
File File文件=新文件(“ScreenShot”+i+“.PNG”)当然这样的文件不存在。您还必须在此处添加dirPath,正如稍后在
File File File=new File(dirPath,fileName)中所做的那样进一步:如果文件“0”存在(),则将所有后续文件存储为“1”。你应该在那里做一个循环。为你的文件指定一个唯一的名称