copy asset file from one location to new location - Android App

Android examples for App:Assets File

Description

copy asset file from one location to new location

Demo Code


//package com.java2s;
import java.io.File;
import java.io.FileOutputStream;

import java.io.InputStream;
import android.content.Context;

public class Main {

    public static File copy(Context context, String fileName,
            String destPath) {//from   w ww.  j av  a  2s .  co  m
        try {
            InputStream is = context.getAssets().open(fileName);
            File file = new File(destPath);
            FileOutputStream fos = new FileOutputStream(file);
            byte[] buffer = new byte[1024];
            int len = 0;

            while ((len = is.read(buffer)) != -1) {
                fos.write(buffer, 0, len);
            }
            fos.flush();
            fos.close();
            is.close();
            return file;
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }

    }
}

Related Tutorials