「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など