Copy the asset file specified by path to app's data directory. - Android App

Android examples for App:Assets File

Description

Copy the asset file specified by path to app's data directory.

Demo Code


//package com.java2s;
import android.content.Context;

import android.util.Log;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

public class Main {
    /**/*from ww w. ja v a  2 s  . c o  m*/
     * Copy the asset file specified by path to app's data directory. Assumes
     * parent directories have already been created.
     *
     * @param path Path to asset, relative to app's assets directory.
     */
    private static void copyFileAsset(Context context, String out_dir,
            String path) {
        File file = new File(out_dir, path);
        try {
            InputStream in = context.getAssets().open(path);
            OutputStream out = new FileOutputStream(file);
            byte[] buffer = new byte[1024 * 256];
            int read = in.read(buffer);
            while (read != -1) {
                out.write(buffer, 0, read);
                read = in.read(buffer);
            }
            out.close();
            in.close();
        } catch (IOException e) {
            Log.e("ERROR", e.toString());
        }
    }
}

Related Tutorials