Due to the fact that I do not know, it is impossible to use file.rename () to move files between different installed directories on Android (for example, sdcard0 and sdcard1), here is my solution and it works for me:
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+", "")); }
source share