Programmatically move files from cache directory to sdcard

I am trying to programmatically move files from internal memory in Android to an existing directory on an SD card.
I tried two ways. In the first, I used File.renameTo:

String destName = externalDirPath + File.separatorChar + destFileName;
File originFile = new File(cacheDirPath + File.separatorChar + originalfileName);

originFile.renameTo(new File(destName));

In another, I used Runtime.getRuntime ():

Process p = Runtime.getRuntime().exec("/system/bin/sh -");
DataOutputStream os = new DataOutputStream(p.getOutputStream());

String command = "cp " + cacheDirPath + "/" + originalfileName+ " " + externalDirPath + "/" + destFileName+ "\n";

os.writeBytes(command);

They don’t work with both ..

Any suggestion?

+3
source share
1 answer

According to the Android API reference renameTo,

Both paths are at the same mount point. In Android applications, this limitation is likely to hit when trying to copy between the internal storage and the SD card.

, , File byte[], File. .

+5

All Articles