//filePath是指定的路径
private String getImgName(String filePath) {
File dir = new File(filePath); //指定路径
File files[] = dir.listFiles();//得到指定路径下所有文件
List<String> fileNames = new ArrayList<String>();
String newName = null;
for (int i = 0; i < files.length; i++) {
String fileName = files[i].getName();//得到文件的文件名
fileNames.add(fileName);
}
//使用UUID,生成新的不重复的文件名
while (true) {
String uuid = UUID.randomUUID().toString();
newName = uuid.substring(0, 8) + uuid.substring(9, 13)
+ uuid.substring(14, 18) + uuid.substring(19, 23)
+ uuid.substring(24);
if (!fileNames.contains(newName)) {
return newName;
}
}
}