screenshot saving as autogenerated file name

前端 未结 2 2090
难免孤独
难免孤独 2020-12-07 06:22

I made a button to take the screenshot and save into Pictures folder. I set it as being saved under the name capture.jpeg but I wanted it to be saved as such as cafe001.jpeg

相关标签:
2条回答
  • 2020-12-07 07:10

    To save as another name just change the string "capture.jpeg"

    If you want to have it as cafeXXX.jpeg (where XXX is a number) then you could do something like this (this method could potentially cause number overlaps however if files are deleted):

    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");
    

    If you want a timeformat just use SimpleDateFormat changing the format String as required (as only going to day will mean you will only get time format per day)

    String timeFileName = new SimpleDateFormat("yyyy-MM-dd").format(new Date())
    //...other code
    fos = new FileOutputStream(picturesDir.toString() + timeFileName+".jpeg");
    
    0 讨论(0)
  • 2020-12-07 07:14

    You basically have a couple of choices...

    You could...

    List all the files in the directory and simple increment the file count by 1 and use that...

    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");
    

    This, of course, doesn't take into account if a file with the same index exists...

    You could...

    List all the files, sort them, grab the last one, find it's index value and increment that...

    Something like...

    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"
    

    You could...

    Simply create a loop and loop until you find an empty index position...for example see, Java autogenerate directories if exists

    0 讨论(0)
提交回复
热议问题