copy file from Asset to a new location - Android App

Android examples for App:Assets File

Description

copy file from Asset to a new location

Demo Code


//package com.java2s;
import android.content.Context;
import android.content.res.AssetManager;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;

public class Main {
    /**/*from   ww w .j av a  2 s . co  m*/
     *
     *
     * @param context
     * @param assetFilename
     * @param out
     * @throws IOException
     */
    public static void copy(Context context, String assetFilename, File out)
            throws IOException {
        if (context == null) {
            return;
        } else if (out == null) {
            return;
        }

        if (!out.getParentFile().exists()) {
            out.getParentFile().mkdirs();
        }

        AssetManager assetManager = context.getAssets();
        InputStream is = null;
        FileOutputStream fos = null;
        try {
            is = assetManager.open(assetFilename);
            fos = new FileOutputStream(out);
            byte[] buf = new byte[1024];
            int len = -1;
            while ((len = is.read(buf, 0, 1024)) != -1) {
                fos.write(buf, 0, len);
            }
        } finally {
            if (fos != null)
                fos.close();

            if (is != null)
                is.close();
        }
    }
}

Related Tutorials