【问题标题】:Android/Java - When a file exists, rename the fileAndroid/Java - 当文件存在时,重命名文件
【发布时间】:2013-05-19 11:54:13
【问题描述】:

我的逻辑似乎不正确,如果说“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
  • photo1.jpg
  • photo11.jpg
  • photo111.jpg 等

【问题讨论】:

  • 保留原始字符串值photo。始终将您的号码附加到该号码。您正在使用各种 StringBuffers 和单独的连接步骤。只需使用+ 使其更具可读性。
  • 您好@SotiriosDelimanolis,非常感谢您的回复。您能否提供一个代码示例来说明您的意思?干杯。
  • String p = "photo"; p = p + 1 + ".jpg";1 替换为您的计数器。
  • 您在代码中所做的是插入数字而不是替换它。在位置 5,您告诉它添加一个新字符,在本例中为 1,所以它是“photo1”,然后是“photo11”,依此类推。
  • 感谢各位的回复。嘿@mrres1,谢谢你的回答。我现在确实看到了问题!我将如何更换号码?干杯。

标签: java android file rename


【解决方案1】:

您的文件名始终基于i,但当您发现i 的值被使用时,您永远不会更改该值。

【讨论】:

  • 感谢您的回复,约翰。对我当前的问题有什么建议(上面编辑过的问题)?我一直在连接'1',看起来......嗯......
【解决方案2】:

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

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 的做法。

【讨论】:

    【解决方案3】:
    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();
                }
            }
        }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2012-03-06
      • 1970-01-01
      • 2015-06-18
      • 2019-07-22
      • 2017-01-04
      • 2016-05-19
      • 2015-02-06
      相关资源
      最近更新 更多