对于那些我不知道的原因,这是不可能的使用file.rename()之间不同的安装可怕的移动文件(如sdcard0和sdcard1),这里是我的解决方案,它适用于我:
if(canRename(f1, f2)) {
if(!f1.renameTo(f2)) {
Log.e(TAG, "Error to move new app: " + f1 + " > " + f2);
}
} else {
try {
copy(f1, f2);
f1.delete();
} catch (Exception ex) {
Log.e(TAG, "Error to move new app: " + f1 + " > " + f2);
}
}
private void copy(final File f1, final File f2) throws IOException {
f2.createNewFile();
final RandomAccessFile file1 = new RandomAccessFile(f1, "r");
final RandomAccessFile file2 = new RandomAccessFile(f2, "rw");
file2.getChannel().write(file1.getChannel().map(FileChannel.MapMode.READ_ONLY, 0, f1.length()));
file1.close();
file2.close();
}
private boolean canRename(final File f1, final File f2) {
final String p1 = f1.getAbsolutePath().replaceAll("^(/mnt/|/)", "");
final String p2 = f2.getAbsolutePath().replaceAll("^(/mnt/|/)", "");
return p1.replaceAll("\\/\\w+", "").equals(p2.replaceAll("\\/\\w+", ""));
}