Android: Copy Asset To SD Card

Assets folder is where you can store some files for you app. Today I’m going to show you how to copy files from your android app’s asset’s folder to the device SD card. I used this when I have to deploy some default text files for my app and manipulate it at later time. Some existing android applications do this, they create folders with files inside on the first install. You can do the check if the file exists in the directory so you won’t have to copy the assets files every time.
By the way, the assets folder is included in your project package. If you’re using eclipse, you can see it in the package explorer. It looks like this:

An example assets folder. Image from http://goo.gl/ml9UD

An example assets folder. Image from http://goo.gl/ml9UD

In our example our assets has MyHtmlFiles folder with CodeOfANinja.html and ThankYou.html inside. Okay, so now we are going to code the app that:

  1. Copies 2 HTML files from the assets.
  2. Store to the device root directory.

If the HTML files were copied to your SD card, you can now manipulate its contents, rewrite it or make it a dynamic content and show it to your user anytime using the WebView. You can use this code for that.
Download code here:

Download Code

MainActivity.java code

package com.example.assetstosdcard;

import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

import android.os.Bundle;
import android.os.Environment;
import android.app.Activity;
import android.content.res.AssetManager;

public class MainActivity extends Activity {

    // Buffer size used.
    private final static int BUFFER_SIZE = 1024;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        try {
            AssetManager assetFiles = getAssets();

            // MyHtmlFiles is the name of folder from inside our assets folder
            String[] files = assetFiles.list("MyHtmlFiles");

            // Initialize streams
            InputStream in = null;
            OutputStream out = null;

            for (int i = 0; i < files.length; i++) {

                if (files[i].toString().equalsIgnoreCase("images")
                        || files[i].toString().equalsIgnoreCase("js")) {

                    
                     //  @Do nothing. images and js are folders but they will be
                     //  interpreted as files.
                      
                     //  @This is to prevent the app from throwing file not found
                     // exception.
                     

                } else {

                    
                     // @Folder name is also case sensitive
                     // @MyHtmlFiles is the folder from our assets
                     
                    in = assetFiles.open("MyHtmlFiles/" + files[i]);

                    
                     // Currently we will copy the files to the root directory
                     // but you should create specific directory for your app
                     
                    out = new FileOutputStream(
                            Environment.getExternalStorageDirectory() + "/"
                                    + files[i]);
                    copyAssetFiles(in, out);

                }
            }

        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (NullPointerException e) {
            e.printStackTrace();
        } catch (Exception e) {
            e.printStackTrace();
        }

    }

    private static void copyAssetFiles(InputStream in, OutputStream out) {
        try {

            byte[] buffer = new byte[BUFFER_SIZE];
            int read;

            while ((read = in.read(buffer)) != -1) {
                out.write(buffer, 0, read);
            }

            in.close();
            in = null;
            out.flush();
            out.close();
            out = null;

        } catch (IOException e) {
            e.printStackTrace();
        } catch (NullPointerException e) {
            e.printStackTrace();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

}

ActivityMain.xml code – This will what will be seen when our the code runs.

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:layout_centerVertical="true"
        android:gravity="center"
        android:text="CodeOfANinja.html and ThankYou.html should be copied to the root directory of your SD card by now."
        tools:context=".MainActivity" />

</RelativeLayout>

AndroidManifest.xml code – permission is WRITE_EXTERNAL_STORAGE since we are writing files to SD card.

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.assetstosdcard"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />

    <uses-sdk
        android:minSdkVersion="8"
        android:targetSdkVersion="15" />

    <application
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name=".MainActivity"
            android:label="@string/title_activity_main" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>

When our code runs:

Check your SD card when you see this.

Check your SD card when you see this.

Thanks for reading this Android: Copy Asset To SD Card!

19 replies
  1. DM Espiritu
    DM Espiritu says:

    Wow, I am amazed of how great your blog is right now! … grabe you are a real coder…, how sad that I ended up not being a code (maybe its not for me talaga) but I’m happy with my work. :)

    Reply
  2. Mike Dalisay
    Mike Dalisay says:

    @DM: Thanks a lot! It feels good to give something back and this is one of my way – blogging. Haha! You also have a great blog too even though it’s not about codes! I’m glad to hear that you’re happy with your work now! :)

    Reply
  3. zfc
    zfc says:

    Hi, I’m trying to implement your code to my existing project. I did it by copying MainActivity.java and activity_main.xml to my project. However, I am not sure on how to edit my AndroidManifest fail. I copied the code but can’t seem to get it work. I’m new in developing android app. Hope you can give me some guidance. Thank you.

    Reply
  4. zfc
    zfc says:

    If I want to copy the file to a directory which does not exist. How should I modify the code so that a new directory is created and the file is copy to it?

    Reply
  5. MGH
    MGH says:

    Hi, Thank you for your very good and useful tutorial
    I have a question about this
    How should I handle the devices without sdcard?is there any way to determine the existance of a sdcard?or can I copy the files from assets folder to a somewhere else like internal device storage or android root or android data directory?

    Reply
  6. Richard Chan
    Richard Chan says:

    Hello I got two question here want to ask.
    First can i know how to modify the coding on how copy pdf files from assets folder to SD card and then opens in pdf veiwer apps ?

    Is it ok i use android storing blob in sqlite and retrieving it or using content provider ?

    Reply
  7. Richard Chan
    Richard Chan says:

    Hi, Can i know how to copy pdf files from assets folder to SD card and then opens in pdf viewer apps ?

    Is it ok i use android storing blob in sqlite and retrieving it or content provider?

    Reply
  8. ninjazhai
    ninjazhai says:

    You should get the package name of the pdf viewer you want and specify it to the intent. I think it is better to save a file in sdcard than sqlite since sqlite database storage is more limited than sdcard…

    Reply
  9. Nhel
    Nhel says:

    I’ve been dying this for the past 3 days and i always got an error until i found this. Thank you very much. Your code works like a charm. Keep it up :)

    Reply

Leave a Reply

Want to join the discussion?
Feel free to contribute!

Leave a Reply

Your email address will not be published. Required fields are marked *