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:
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:
- Copies 2 HTML files from the assets.
- 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:
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:
Thanks for reading this Android: Copy Asset To SD Card!
19 responses to “Android: Copy Asset To SD Card”
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. :)
@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! :)
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.
Hi zfc, on your AndroidManifest.xml, you actually just have to add
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?
you have to create the directory first using the mkdirs method, you can see (TextFileHelper.java) the createPath() method in this post http://www.codeofaninja.com/2013/01/working-with-text-files-in-android.html
Hi Mike, is there a way that this can be implemented on Ionic app? thanks.
Hi @disqus_1qU26Um9Br:disqus, I haven’t really used the Ionic framework, but you can call an Android method via JavaScript, so I guess it is possible.
thank you so much for the reply. will explore on this and get back to you. cheers.
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?
Hi @654357408446f4115e45a9a713882065:disqus, how about this? http://stackoverflow.com/questions/902089/how-to-tell-if-the-sdcard-is-mounted-in-android
How I should modify the code if I want to copy the files to internal storage instead? The code below will only copy the database files. How if I want to copy all the files in a subfolder in asset?
http://developergoodies.blogspot.com/2012/11/copy-android-asset-to-internal-storage.html
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 ?
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?
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…
good!
Thanks for reading our post @disqus_9XxW36qQIu:disqus!
How to copy an xml file from the root folder( i.e /data/data/com.packagename) to sdcard via java code
Without root plz with path included
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 :)