我正在尝试在Android中创建应用,其部分功能是将.jpg
文件扩展名重命名为.jpeg
文件扩展名。但是,它没有用。
filepath
是.jpg
文件的路径,doThings()
是文件重命名后的作用。
这是我的代码:
// Create new string to store edited file path
String newfilepath = filepath;
// Create new file to be used for renaming
File file1 = new File(filepath);
// Remove JPG extension
newfilepath = newfilepath.substring(0, newfilepath.length() - 3);
// Replace with JPEG extension
newfilepath += "jpeg";
// Add new file for renaming purposes
File file2 = new File(newfilepath);
// Rename file from JPG to JPEG
boolean rename = file1.renameTo(file2);
// Check if file renaming was successful
if(rename) {
// Does things
doThings(newfilepath);
}
注意:我也尝试将file1.renameTo(file2);
更改为:
boolean test = file1.renameTo(file2);
System.out.println("Renamed? " + test);
在logcat中收到了这个:
I/System.out: Renamed? false
另外,为了证明这不是权限问题,这里是AndroidManifest.xml
文件:
<uses-permission
android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission
android:name="android.permission.READ_EXTERNAL_STORAGE" />
我确实在这里请求Android 6+格式的权限:
public void getPermissions(View view) {
String[] perms = { Manifest.permission.READ_EXTERNAL_STORAGE, Manifest.permission.WRITE_EXTERNAL_STORAGE };
if (EasyPermissions.hasPermissions(this, perms)) {
// We have permissions, send message
Toast.makeText(this, "Select an image.", Toast.LENGTH_SHORT).show();
selectFile();
} else {
// We don't have permissions
Toast.makeText(this, "Permissions are required", Toast.LENGTH_SHORT).show();
// Ask again
ActivityCompat.requestPermissions(MainActivity.this,
perms, PERMISSIONS_MULTIPLE_REQUEST);
}
}
感谢帮助,谢谢!
编辑:我一直在测试并得到奇怪的结果。我今天晚些时候会更新。