Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to move/rename file from internal app storage to external storage on Android?

Tags:

java

file

android

I am downloading files from the internet and saving the streaming data to a temp file in my app's internal storage given by getFilesDir().

Once the download is complete, I need to move the temp file to my download directory on External Memory (usually an SD Card). For some reason though, File.renameTo() isn't working for this. I'm guessing there's a problem because it's two separate file systems, but I can still download directly to the SD Card and the file URIs are correct.

Is there another simple and quick way to transfer that file from internal memory to external or do I have to do a byte stream copy and delete the original?

like image 926
CodeFusionMobile Avatar asked Jan 22 '11 19:01

CodeFusionMobile


People also ask

At which location files are stored in Android?

Android provides two types of physical storage locations: internal storage and external storage. On most devices, internal storage is smaller than external storage. However, internal storage is always available on all devices, making it a more reliable place to put data on which your app depends.

What is app specific storage Android?

The system provides directories within external storage where an app can organize files that provide value to the user only within your app. One directory is designed for your app's persistent files, and another contains your app's cached files.


1 Answers

To copy files from internal memory to SD card and vice-versa using following piece of code:

public static void copyFile(File src, File dst) throws IOException {     FileChannel inChannel = new FileInputStream(src).getChannel();     FileChannel outChannel = new FileOutputStream(dst).getChannel();     try     {         inChannel.transferTo(0, inChannel.size(), outChannel);     }     finally     {         if (inChannel != null)             inChannel.close();         if (outChannel != null)             outChannel.close();     } } 

And - it works...

like image 165
Barmaley Avatar answered Oct 07 '22 05:10

Barmaley