copy Data From Asset To Path - Android App

Android examples for App:Assets

Description

copy Data From Asset To Path

Demo Code


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

import android.content.Context;

public class Main {
  public static void copyDataFromAssetToPath(Context context, String assetName,
      File path) throws IOException {

    InputStream mInput;/*from w w  w  .java2  s  .  c  o m*/
    try {
      mInput = context.getAssets().open(assetName);
    } catch (IOException e) {
      throw e;
    }

    OutputStream mOutput;
    try {
      path.getParentFile().mkdirs();
      mOutput = new FileOutputStream(path);
    } catch (FileNotFoundException e) {
      throw e;
    }
    byte[] buffer = new byte[1024];
    int size;
    while ((size = mInput.read(buffer)) > 0) {
      mOutput.write(buffer, 0, size);
    }

    // Close the streams
    mOutput.flush();
    mOutput.close();
    mInput.close();
  }
}

Related Tutorials