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 |
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.
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. |
The Code of a Ninja Resources
For FREE programming tutorials, click the red button below and subscribe! :)
website