write Asset To Cache File - Android App

Android examples for App:Assets File

Description

write Asset To Cache File

Demo Code


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

import android.content.res.AssetManager;

public class Main {
  public static File writeAssetToFile(String assetName, File targetFile,
      int mode) {
    return writeAssetToFile(null, assetName, targetFile, mode);
  }//from www  . j a va2s . c  om

  public static File writeAssetToFile(AssetManager assets, String assetName,
      File targetFile, int mode) {
    try {
      InputStream in = assets.open(assetName);
      FileOutputStream out = new FileOutputStream(targetFile);

      byte[] buffer = new byte[1024];
      int len;
      while ((len = in.read(buffer)) > 0) {
        out.write(buffer, 0, len);
      }
      in.close();
      out.close();

      // setPermissions(targetFile.getAbsolutePath(), mode,
      // -1, -1);

      return targetFile;
    } catch (IOException e) {
      if (targetFile != null)
        targetFile.delete();

      return null;
    }
  }
}

Related Tutorials