1

我似乎无法正确理解我的逻辑,如果说“photo.jpg”和“photo1.jpg”存在,我正在尝试将文件重命名为“photo2.jpg”,依此类推。

在我运行代码并拍照的那一刻,只有“photo.jpg”和“photo1.jpg”存在,然后如果拍摄第三张和第四张等照片,它们就会被覆盖。

String photoName = "photo.jpg";
        String i = "0";
        int num = 0;

        File photo = new File(Environment.getExternalStorageDirectory(), photoName);

        //for (File file : photo.listFiles()){
        while(photo.exists()) {             
            //if(file.getName().equals("photo.jpg")){
                //photo.delete();
                num = Integer.parseInt(i);
                ++num;
                String concatenatedNum = Integer.toString(num);

                StringBuffer insertNum = new StringBuffer(photoName);
                insertNum.insert(5, concatenatedNum);

                photoName = insertNum.toString();

                photo.renameTo(new File(Environment.getExternalStorageDirectory(), photoName));
            //}
        }


        try {
            FileOutputStream fos=new FileOutputStream(photo.getPath());

            //MediaStore.Images.Media.insertImage(getContentResolver(), yourBitmap, yourTitle, yourDescription);

            //write jpeg to local drive
            fos.write(jpeg[0]);
            fos.close();
        }
        catch (java.io.IOException e) {}

感谢您的时间和帮助!


编辑:解决了一半:我意识到我正在覆盖文件而不是创建一个新文件。现在我可以拍摄多张照片,并将它们保存为自己的文件。但是,文件的命名现在是:

  • 照片.jpg
  • 照片1.jpg
  • 照片11.jpg
  • photo111.jpg等
4

3 回答 3

1

您始终将文件名基于i,但您永远不会更改i使用该数字时的值。

于 2013-05-23T23:38:38.707 回答
1

我知道这是较旧的,但是当我寻找解决方案时,我最终来到了这里。我最终做了以下事情:

String baseFilename = "photo";
File outputFile = new File(Environment.getExternalStorageDirectory(), baseFilename + ".jpg");
int i = 2; // whatever increment you want to start with, I'm copying Windows' naming convention
while (outputFile.exists()){
    outputFile = new File(Environment.getExternalStorageDirectory(), baseFilename + "(" + i + ")" + ".jpg");
    i++;
}

你最终会得到 photo.jpg、photo(2).jpg、photo(3).jpg 等。

显然,您可以轻松更改 int 的附加方式,但就像我说的那样,我只是决定遵循 Windows 的操作方式。

于 2015-05-16T22:05:22.237 回答
0
private void savePhoto(String fileName, final String extension)
{
    // First, get all the file names from the directory
    String[] allFiles = new File(Environment.getExternalStorageDirectory().toString()).list();

    // Create a new empty list to put all the matching file names in
    //  In this case all the files names that are "photo.jpg" or "photo#.jpg"
    ArrayList<String> files = new ArrayList<String>();

    // Skim through all the files
    for(String file : allFiles)
    {
        // Use a regular expression to find matching files
        //  fileName[0-9]+\.extension|fileName\.extension
        if(file.matches(fileName + "[0-9]+\\." + extension + "|" + fileName + "\\." + extension))
        {
            files.add(file);
        }
    }

    files.trimToSize();

    // Now sift through the list and find out what the highest number is
    //  Example, if you've taken 8 photos, then highestNumber will equal 8
    int highestNumber = 0;
    int digit;

    for(String file : files)
    {
        try
        {
            digit = Integer.parseInt(file.replace(fileName, "").replace("." + extension, ""));
        }
        catch(NumberFormatException e)
        {
            digit = 1;
        }

        if(digit > highestNumber)
        {
            highestNumber = digit;
        }
    }

    // Create the file object
    fileName = fileName + highestNumber++ + "." + extension;
    File file = new File(Environment.getExternalStorageDirectory().toString(), fileName);


    // In not sure what you do around here, I can't find any array titled "jpeg"
    //  So do what you will
    FileOutputStream fostream = null;

    try
    {
        fostream = new FileOutputStream(file);

        //MediaStore.Images.Media.insertImage(getContentResolver(), yourBitmap, yourTitle, yourDescription);

        //write jpeg to local drive
        fostream.write(jpeg[0]);

    }
    catch(IOException e)
    {
        e.printStackTrace();
    }
    finally
    {
        if(fostream != null)
        {
            try
            {
                fostream.flush();
                fostream.close();
            }
            catch(IOException e)
            {
                e.printStackTrace();
            }
        }
    }
}
于 2013-05-23T23:59:41.653 回答