copy Assets File - Android android.content

Android examples for android.content:Context

Description

copy Assets File

Demo Code

import android.content.Context;
import android.content.res.AssetManager;
import android.os.Environment;
import android.text.format.DateFormat;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.URLDecoder;
import java.net.URLEncoder;
import java.util.ArrayList;

public class Main{

    public static void copyAssetsFile(Context context, String fileName,
            String targetName) throws IOException {
        File targetFile = new File(targetName);
        if (targetFile.exists()) {
            LogUtil.i("target file is exists!");
            return;
        }//from   ww w.jav a  2s .c o  m
        AssetManager am = context.getAssets();
        InputStream source = am.open(fileName);
        BufferedInputStream inBufferedInputStream = new BufferedInputStream(
                source);

        targetFile.createNewFile();
        FileOutputStream outputStream = new FileOutputStream(targetFile);
        BufferedOutputStream outBufferedOutputStream = new BufferedOutputStream(
                outputStream);

        byte[] b = new byte[1024 * 4];
        int len;
        while ((len = inBufferedInputStream.read(b)) != -1) {
            outBufferedOutputStream.write(b, 0, len);
        }
        outBufferedOutputStream.flush();
        inBufferedInputStream.close();
        outBufferedOutputStream.close();
        outputStream.close();
        source.close();
    }

}

Related Tutorials