屏幕截图另存为自动生成的文件名

时间:2013-08-19 22:34:24

标签: java filenames

我做了一个按钮来截取屏幕截图并保存到Pictures文件夹中。我将其设置为以capture.jpeg名称保存,但我希望将其保存为cafe001.jpeg,cafe002.jpeg,就像这样。还请你告诉我如何将其保存为时间格式.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();
        }
    });

2 个答案:

答案 0 :(得分:0)

要另存名称,只需更改字符串"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");

答案 1 :(得分:0)

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

你可以......

列出目录中的所有文件,然后简单地将文件数增加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