【问题标题】:screenshot saving as autogenerated file name屏幕截图另存为自动生成的文件名
【发布时间】:2013-08-19 22:34:23
【问题描述】:

我制作了一个按钮来截取屏幕截图并保存到图片文件夹中。我将它设置为以 capture.jpeg 的名称保存,但我希望它像这样保存,例如 cafe001.jpeg、cafe002.jpeg。另外请告诉我如何将其保存为时间 format.jpeg ? 提前感谢您的帮助

container = (LinearLayout) findViewById(R.id.LinearLayout1);
        Button captureButton = (Button) findViewById(R.id.captureButton);
        captureButton.setOnClickListener(new OnClickListener() {
        public void onClick(View v) {
            container.buildDrawingCache();
            Bitmap captureView = container.getDrawingCache();
            FileOutputStream fos;
            try {
                fos = new FileOutputStream(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES).toString() + "capture.jpeg");
                captureView.compress(Bitmap.CompressFormat.JPEG, 100, fos);
            } catch (FileNotFoundException e) {
                e.printStackTrace();
            }
            Toast.makeText(getApplicationContext(),
                    "Captured under Pictures drectory", Toast.LENGTH_LONG)
                    .show();
        }
    });

【问题讨论】:

    标签: java filenames


    【解决方案1】:

    你基本上有几个选择......

    你可以...

    列出目录中的所有文件并将文件计数简单地增加 1 并使用它...

    File[] files = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES).toString()).
        listFiles(new FileFilter() {
            public boolean accept(File pathname) {
                String name = pathname.getName();
                return pathname.isFile() && name.toLowerCase().startsWith("capture") && name.toLowerCase().endsWith(".jpeg");
            }
    });
    
    int fileCount = files.length();
    
    fos = new FileOutputStream(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES).toString() + 
        "capture" + fileCount + ".jpeg");
    

    当然,这并没有考虑是否存在具有相同索引的文件...

    你可以...

    列出所有文件,对它们进行排序,抓取最后一个,找到它的索引值并增加它...

    类似...

    File[] files = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES).toString()).
        listFiles(new FileFilter() {
            public boolean accept(File pathname) {
                String name = pathname.getName();
                return pathname.isFile() && name.toLowerCase().startsWith("capture") && name.toLowerCase().endsWith(".jpeg");
            }
    });
    
    Arrays.sort(files);
    File last = files[files.length - 1];
    
    Pattern pattern = Pattern.compile("[0-9]+");
    Matcher matcher = pattern.matcher(last.getName());
    
    int index = 1;
    if (matcher.find()) {
        String match = matcher.group();
        index = Integer.parseInt(match) + 1;
    }
    
    String fileName = "capture" + index + ".jpeg"
    

    你可以...

    只需创建一个循环并循环,直到找到一个空的索引位置...例如,请参阅Java autogenerate directories if exists

    【讨论】:

      【解决方案2】:

      要另存为其他名称,只需更改字符串"capture.jpeg"

      如果你想将它作为 cafeXXX.jpeg(其中 XXX 是一个数字),那么你可以这样做(这种方法可能会导致数字重叠,但是如果文件被删除):

      int count = 1;
      File picturesDir = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES);
      File[] content = picturesDir.listFiles();
      for (File f: content)
      {
          if (f.getName().matches("cafe\\d+\\.jpeg"))
              count++;
      }
      //... your other code
      // if leading zeros important then add formatting code to the count
      fos = new FileOutputStream(picturesDir.toString() + "cafe"+count+".jpeg");
      

      如果您想要时间格式,只需使用 SimpleDateFormat 根据需要更改格式字符串(因为只有一天意味着您每天只能获得时间格式)

      String timeFileName = new SimpleDateFormat("yyyy-MM-dd").format(new Date())
      //...other code
      fos = new FileOutputStream(picturesDir.toString() + timeFileName+".jpeg");
      

      【讨论】:

        猜你喜欢
        • 2021-04-16
        • 2014-05-05
        • 2015-05-19
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2013-08-22
        • 1970-01-01
        相关资源
        最近更新 更多