最近项目维护 更新 发现了反馈的问题
在正式环境下 图片格式为JPEG的图片无法完成上传, 由于app是定制开发, 服务器并不在我们公司 ,所以只有我这个小小的android来改变图片格式(脏话)
话不多话上代码
1.图片(File)
//这里并没有对pathFile文件是否存在做判断 需要的可以自行判断
private File createFile(File pathFile) {
//图片命名的时间戳
long totalMilliSeconds = System.currentTimeMillis();
String str = String.valueOf(totalMilliSeconds);
//获取文件名字
String fileName = pathFile.getName();
String s = fileName.toLowerCase();
if (s.contains(".jpeg")) {
String oldPath = pathFile.getAbsolutePath();
if (!TextUtils.isEmpty(oldPath) && !TextUtils.isEmpty(fileName)) {
String newPath = pathFile.getAbsolutePath() + ".png";
newPath = oldPath.replace(fileName, str + ".jpg");
File file2 = renameFile(oldPath, newPath);
return file2;
} else {
return null;
}
} else {
return pathFile;
}
}
2.0 重命名文件
/**
* oldPath 和 newPath必须是新旧文件的绝对路径
*/
private File renameFile(String oldPath, String newPath) {
if (TextUtils.isEmpty(oldPath)) {
return null;
}
if (TextUtils.isEmpty(newPath)) {
return null;
}
File oldFile = new File(oldPath);
File newFile = new File(newPath);
boolean b = oldFile.renameTo(newFile);
File file2 = new File(newPath);
return file2;
}
随手记录 仅供参考()