Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Programmatically move files from cache directory to SDCard

I'm trying to programmatically move files from the internal memory in Android to a existing directory in the SD card.
I tried two ways. In the first one I used File.renameTo:

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

originFile.renameTo(new File(destName));

In the other 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);

With both of them it doesn't work..

Any suggestion?

like image 717
dany.bony Avatar asked May 18 '12 16:05

dany.bony


People also ask

How do I move my cache to my SD card?

Press “Source” and indicate the folder with the app's cache. It is located in Android/obb/the folder with the app's name. Press “Destination” to choose a folder on the SD card for the transfer. After every field is filled out, press the check mark in the top right corner and select the “pin” next to the Name field.

What is stored in a cache directory?

The cache directory is where files are stored during background uploads and after upload for quick access should you need to open the file from the network drive (Jungle Disk (Formerly Workgroup) and Desktop Edition users only).

What is cache directory in Android?

Your Android phone's limited storage can fill up quickly. And one cause of this that's easy to overlook is the stored information that apps regularly create to run at their best. These temporary data files are known as a cache; a fair chunk of your Android phone's storage space might be filled up with cache files.


1 Answers

According to the Android API Reference of renameTo,

Both paths be on the same mount point. On Android, applications are most likely to hit this restriction when attempting to copy between internal storage and an SD card.

You will probably have to read the File into a byte[] and then write it into a new File. This answer covers it.

like image 158
frapontillo Avatar answered Sep 19 '22 13:09

frapontillo