Example usage for android.content Context openFileOutput

List of usage examples for android.content Context openFileOutput

Introduction

In this page you can find the example usage for android.content Context openFileOutput.

Prototype

public abstract FileOutputStream openFileOutput(String name, @FileMode int mode) throws FileNotFoundException;

Source Link

Document

Open a private file associated with this Context's application package for writing.

Usage

From source file:Main.java

public static boolean saveObject(Context context, Serializable ser, String file) {
    FileOutputStream fos = null;//  www.  java  2 s .c  o  m
    ObjectOutputStream oos = null;
    try {
        fos = context.openFileOutput(file, Context.MODE_PRIVATE);
        oos = new ObjectOutputStream(fos);
        oos.writeObject(ser);
        oos.flush();
        return true;
    } catch (Exception e) {
        e.printStackTrace();
        return false;
    } finally {
        try {
            oos.close();
        } catch (Exception e) {
        }
        try {
            fos.close();
        } catch (Exception e) {
        }
    }
}

From source file:Main.java

public static void writeMediaListLastUpdatedTimeToFile(Context context, String listType,
        String lastUpdatedTime) {

    // writing to internal private memory
    FileOutputStream fos = null;//from w  w  w.ja  va  2  s .  c o m
    try {
        fos = context.openFileOutput(listType, Context.MODE_PRIVATE);
        fos.write(lastUpdatedTime.getBytes());
        fos.close();
        Log.v(TAG, "Lastupdated time " + lastUpdatedTime + " successfully written to file for listType = "
                + listType);

    } catch (Exception exp) {
        Log.v(TAG, "Failed to write lastUpdated time. exp:" + exp.getMessage());
        exp.printStackTrace();
    }
}

From source file:Main.java

public static void write(Context context, String fileName, String content) {
    if (content == null) {
        content = "";
    }/*  ww w . java  2 s.com*/
    try {
        FileOutputStream fs = context.openFileOutput(fileName, Context.MODE_PRIVATE);
        fs.write(content.getBytes());
        fs.close();
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }
}

From source file:Main.java

public static boolean writeFile(Context context, String fileName, String content) {
    boolean success = false;
    FileOutputStream fos = null;/*from ww w.ja v  a 2 s.c o  m*/
    try {
        fos = context.openFileOutput(fileName, Context.MODE_PRIVATE);
        byte[] byteContent = content.getBytes();
        fos.write(byteContent);

        success = true;
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    } finally {
        try {
            if (fos != null)
                fos.close();
        } catch (IOException ioe) {
            ioe.printStackTrace();
        }
    }

    return success;
}

From source file:Main.java

public static void write(Context context, String fileName, String content) {
    if (content == null)
        content = "";

    try {/* w  ww  .  j a va  2s .co m*/
        FileOutputStream fos = context.openFileOutput(fileName, Context.MODE_PRIVATE);
        fos.write(content.getBytes());

        fos.close();
    } catch (Exception e) {
        e.printStackTrace();
    }
}

From source file:Main.java

public static void saveImage(Context context, String fileName, Bitmap bitmap, int quality) throws IOException {
    if (bitmap == null || fileName == null || context == null)
        return;//from  w w w  .jav  a 2 s.c o m

    FileOutputStream fos = context.openFileOutput(fileName, Context.MODE_PRIVATE);
    ByteArrayOutputStream stream = new ByteArrayOutputStream();
    bitmap.compress(Bitmap.CompressFormat.JPEG, quality, stream);
    byte[] bytes = stream.toByteArray();
    fos.write(bytes);
    fos.close();
}

From source file:Main.java

public static void saveImage(Context context, String fileName, Bitmap bitmap, int quality) throws IOException {
    if (bitmap == null || fileName == null || context == null)
        return;//from   ww  w.  j a  va2  s  .  co m

    FileOutputStream fos = context.openFileOutput(fileName, Context.MODE_PRIVATE);
    ByteArrayOutputStream stream = new ByteArrayOutputStream();
    bitmap.compress(CompressFormat.JPEG, quality, stream);
    byte[] bytes = stream.toByteArray();
    fos.write(bytes);
    fos.close();
}

From source file:Main.java

/**
 * (android) write to file//from  w w  w . j a v  a2 s . c om
 * 
 * @param fileName
 * @param toSave
 * @return
 */
public static boolean androidFileSave(Context con, String fileName, String toSave) {
    Properties properties = new Properties();
    properties.put(FILE_ENCODING, toSave);
    try {
        FileOutputStream stream = con.openFileOutput(fileName, Context.MODE_WORLD_WRITEABLE);
        properties.store(stream, "");
    } catch (FileNotFoundException e) {
        return false;
    } catch (IOException e) {
        return false;
    }

    return true;
}

From source file:net.giovannicapuano.visualnovel.Memory.java

public static boolean putLastScript(Context context, String script) {
    try {/*from   ww  w .java  2  s .c o  m*/
        FileOutputStream fos = context.openFileOutput(SCRIPT, Context.MODE_PRIVATE);
        fos.write(script.getBytes());
        fos.flush();
        fos.close();

        return true;
    } catch (IOException e) {
        Utils.error(e);
        Utils.error(context, context.getString(R.string.error_saving_game));
        return false;
    }
}

From source file:Main.java

public static void secSet(Context context, String key, String val) {
    //long now = System.currentTimeMillis();
    FileOutputStream fos = null;/*from  w  w w  .  jav a  2 s  .c  om*/
    try {
        DataOutputStream dos = new DataOutputStream(
                (fos = context.openFileOutput(key + ".mits", android.content.Context.MODE_PRIVATE)));
        dos.writeUTF(val);
    } catch (FileNotFoundException e1) {
        //e1.printStackTrace();
    } catch (IOException e1) {
        //e1.printStackTrace();
    } finally {
        if (fos != null)
            try {
                fos.close();
            } catch (IOException ignored) {
            }
    }
}