vinit_ivar
vinit_ivar

Reputation: 650

Move Android app resources to external memory

I'll keep it short - I've got a directory with a bunch of files inside it that my app requires. I need to move these to the SD card, because it's pointless keeping it on internal memory. How do I go about this?

I'm assuming I'll need to keep the directory in res, initially, then open/create the required folder on external memory, and move all the files there.

How do I do this only once? Do I have to check the integrity of the external folder every time I run my app?

Thanks.

Upvotes: 1

Views: 970

Answers (2)

Gilad Haimov
Gilad Haimov

Reputation: 5857

You can easily iterate over each of your res/ folders and, if you wish, copy them to SDCARD. An example for the res/raw folder:

void iterate_over_res_raw() {
    Field[] fields = R.raw.class.getFields();
    for (int count=0; count < fields.length; count++){
        String rawElement = fields[count].getName();
    }
}

And from there copy selected resources to SDCARD.


But this will not solve your problem because you will not be able to delete the res files after copying them. An app may never delete or update its own APK's content. It's a security restriction.


So what can you do:

A. Create an apk with no/minimal resource files and have it download the additional resources from your server and into the SDCARD upon first run.

B. Allow installation of entire APK onto the sdcard by setting installLocation to preferExternal:

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
          package="string"
          .......
          android:installLocation="preferExternal" >

Hope it helps.

Upvotes: 2

Pozzo Apps
Pozzo Apps

Reputation: 1857

You cannot do this using res folder, res folder is designed to be maintained within the apk file.

To make sure your files are saved separated to your app you will need to do it by yourself, downloading your resources after app is installed and saving it where you want, but afterwards you will need to check for integrity and load by yourself as well.

Upvotes: 1

Related Questions