save byte array to a File - Android java.io

Android examples for java.io:FileOutputStream

Description

save byte array to a File

Demo Code


//package com.java2s;

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 ww  . ja  va2  s  . co  m*/
        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