copy Assert To SD Card - Android App

Android examples for App:External Storage

Description

copy Assert To SD Card

Demo Code


import java.io.BufferedReader;
import java.io.File;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.Locale;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.zip.ZipEntry;
import java.util.zip.ZipInputStream;
import android.content.Context;

public class Main{
    static final String TAG = BIOUtilities.class.getSimpleName();
    public final static void copyAssertToSDCard(final Context ctx,
            final String assertPath, final File disPath) {
        if (disPath == null || !disPath.isDirectory()) {
            return;
        }/*from   w w  w  .  j a v a2  s.  co m*/
        try {
            String[] files = ctx.getAssets().list(assertPath);
            if (files.length == 0) {// is file
                copyAssetFile(ctx, assertPath, disPath);
            } else {// is folder
                BLog.i(TAG, "copy folder from " + assertPath + " to "
                        + disPath);
                String[] fileSeg = assertPath.split(File.separator);
                String newFilePath = fileSeg[fileSeg.length - 1];
                File subFolder = new File(disPath, newFilePath);
                if (!subFolder.exists()) {
                    subFolder.mkdir();
                }
                for (String file : files) {
                    copyAssertToSDCard(ctx, String.format("%s%s%s",
                            assertPath, File.separator, file), subFolder);
                }
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    /**
     * 
     * @param ctx
     * @param srcFileName
     * @param dst
     */
    public static final void copyAssetFile(Context ctx, String srcFileName,
            File dst) {
        InputStream is = null;
        OutputStream os = null;
        try {
            if (!dst.exists()) {
                boolean result = dst.mkdirs();
                BLog.i(TAG, "make dir " + dst.getAbsolutePath()
                        + "; result:" + result);
            }
            is = ctx.getAssets().open(srcFileName);
            String[] filePath = srcFileName.split(File.separator);
            String fileName = filePath[filePath.length - 1];
            os = new FileOutputStream(new File(dst, fileName));
            byte[] b = new byte[1024];
            int len;
            while ((len = is.read(b)) != -1) {
                os.write(b, 0, len);
            }
            os.flush();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (is != null) {
                try {
                    is.close();
                } catch (Exception ex) {
                    ex.printStackTrace();
                }
            }
            if (os != null) {
                try {
                    os.close();
                } catch (Exception ex) {
                    ex.printStackTrace();
                }
            }
        }
    }
}

Related Tutorials