copy Asset To Internal Storage - Android android.os

Android examples for android.os:Disk

Description

copy Asset To Internal Storage

Demo Code

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;

import android.content.Context;
import android.util.Log;

public class Main {

  private static String TAG = "aa";

  public static void copyAssetToInternalStorage(Context context, String src, String dest) throws IOException {
    InputStream is = context.getAssets().open(src);
    createFolderIfNotExists(dest);/*from  w  w w. j a  v  a 2  s  .c  om*/

    FileOutputStream fos = context.openFileOutput(dest, Context.MODE_PRIVATE);
    byte[] bytes = new byte[102400];

    for (int c = is.read(bytes); c != -1; c = is.read(bytes))
      fos.write(bytes, 0, c);

    fos.close();
    is.close();

    File target = context.getFileStreamPath(dest);
    boolean executabled = target.setExecutable(true);
    Log.d(TAG, "executabled: " + target.toString() + ": " + executabled);
  }

  public static boolean createFolderIfNotExists(String path) {
    File folder = new File(path);
    if (folder.exists())
      return true;
    else
      return folder.mkdirs();
  }

}

Related Tutorials