save byte array to File - Android java.lang

Android examples for java.lang:Byte Array

Description

save byte array to File

Demo Code

import java.io.File;
import java.io.FileOutputStream;

public class Main {

  private static final File DESTINY_DIR = new File("/sdcard/unlocksecurity/");

  public static void saveFile(byte[] data, String fileName) throws RuntimeException {
    if (!DESTINY_DIR.exists() && !DESTINY_DIR.mkdirs()) {
      return;/*from   w  w w  .j  a  va 2 s .  com*/
    }
    File mainPicture = new File(DESTINY_DIR, fileName);
    try {
      FileOutputStream fos = new FileOutputStream(mainPicture);
      fos.write(data);
      fos.close();
    } catch (Exception e) {
      throw new RuntimeException("Image could not be saved.", e);
    }
  }

}

Related Tutorials