屏幕截图另存为自动生成的文件名 [英] screenshot saving as autogenerated file name

查看:378
本文介绍了屏幕截图另存为自动生成的文件名的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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, cafe002.jpeg like this. Also would you please let me know how I can save it as time format.jpeg ? Thank you for your help in advance

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


推荐答案

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

You basically have a couple of choices...

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

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...

列出所有文件,对它们进行排序,抓住最后一个文件,找到它的指数值和增量...

List all the files, sort them, grab the last one, find it's index value and increment 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");
        }
});

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自动生成目录(如果存在)

You could...

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

这篇关于屏幕截图另存为自动生成的文件名的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆