1

「photo.jpg」と「photo1.jpg」が存在する場合など、ファイルの名前を「photo2.jpg」に変更しようとしています。

コードを実行して写真を撮った瞬間、「photo.jpg」と「photo1.jpg」だけが存在し、3 枚目、4 枚目などの写真が撮られると上書きされます。

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

これは古いことは知っていますが、解決策を探していたときにここにたどり着きました。私は最終的に次のことをしました:

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 に答える
1

ファイル名は常に に基づいていますが、その番号が使用されていることがわかったときにiの値を変更することはありません。i

于 2013-05-23T23:38:38.707 に答える
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 に答える