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 void saveBitmapToCacheDir(Context context, Bitmap bitmap, String name) {
    try {/*www .j  av a 2s.  c om*/
        File file = new File(context.getCacheDir(), name);
        if (file.exists()) {
            file.delete();
        }
        FileOutputStream fos = context.openFileOutput(name, MODE_PRIVATE);
        bitmap.compress(Bitmap.CompressFormat.JPEG, 100, fos);
        fos.flush();
        fos.close();
    } catch (IOException e) {
        e.printStackTrace();
    }
}

From source file:Main.java

public static boolean cache(Context context, String file, byte[] data, int mode) {
    boolean bResult = false;
    if (null != data && data.length > 0) {
        FileOutputStream fos = null;
        try {//  ww  w .j a va  2s  . c o m
            fos = context.openFileOutput(file, mode);
            fos.write(data);
            fos.flush();
            bResult = true;
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (null != fos) {
                try {
                    fos.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }

    return bResult;
}

From source file:Main.java

public static void intArrayToFile(Context myContext, String filename, int[] array) {
    File root = myContext.getFilesDir();
    File current = new File(root, filename);
    current.delete();/*from ww w. j  av a  2 s.c  o m*/
    FileOutputStream outputStream;
    try {
        outputStream = myContext.openFileOutput(filename, Context.MODE_APPEND);
        for (int i : array) {
            String s = "" + i;
            outputStream.write(s.getBytes());
            outputStream.write("\n".getBytes());
        }
        outputStream.close();
    } catch (Exception e) {
        e.printStackTrace();
    }

}

From source file:ca.tbcn.greenp.GreenParkingApp.java

public static void updateJsonFile(Context context, String contents) throws IOException {
    // write out to local file
    FileOutputStream fos = context.openFileOutput(JSON_FILE_NAME, Context.MODE_PRIVATE);
    fos.write(contents.getBytes());/* ww w. ja  v a 2  s  .  c o m*/
    fos.close();
}

From source file:nl.spellenclubeindhoven.dominionshuffle.DataReader.java

public static boolean writeStringToFile(Context context, String filename, String content) {
    OutputStreamWriter out = null;
    try {/*from  www .  ja va  2s  .co m*/
        out = new OutputStreamWriter(context.openFileOutput(filename, Activity.MODE_PRIVATE));
        out.write(content);
    } catch (FileNotFoundException ignore) {
        return false;
    } catch (IOException ignore) {
        return false;
    } finally {
        try {
            if (out != null)
                out.close();
        } catch (IOException ignore) {
            return false;
        }
    }

    return true;
}

From source file:Main.java

public static boolean saveObject(@NonNull Context context, Object obj, String fileName) {
    if (obj == null || TextUtils.isEmpty(fileName)) {
        return false;
    }//from   ww  w. j a v a  2 s  .co m

    FileOutputStream fos = null;
    ObjectOutputStream oos = null;
    try {
        fos = context.openFileOutput(fileName, Context.MODE_PRIVATE);
        oos = new ObjectOutputStream(fos);
        oos.writeObject(obj);
        oos.flush();
        return true;
    } catch (Exception e) {
        e.printStackTrace();
        return false;
    } finally {
        try {
            if (fos != null)
                fos.close();
            if (oos != null)
                oos.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

From source file:com.folio3.parse.MainActivity.java

public static void writeLogToFile(Context context, String alert) {
    String eol = System.getProperty("line.separator");
    BufferedWriter writer = null;
    try {/*  ww  w .  j  a  v a2s .  c om*/
        FileOutputStream openFileOutput = context.openFileOutput(FILE, Context.MODE_APPEND);
        openFileOutput.write((alert + "\r\n").getBytes());
    } catch (Exception e) {
        throw new RuntimeException(e);
    } finally {
        if (writer != null) {
            try {
                writer.close();
            } catch (IOException e) {
                e.printStackTrace();
            }

        }
    }
}

From source file:Main.java

public static void imgCacheWrite(Context context, String cacheImgFileName, String imgBase64Str) {
    File cacheImgFile = new File(context.getFilesDir() + "/" + cacheImgFileName);
    if (cacheImgFile.exists()) {
        cacheImgFile.delete();/*from   ww  w.j  av  a2s .  c om*/
    }

    FileOutputStream fos;
    try {
        fos = context.openFileOutput(cacheImgFileName, Context.MODE_PRIVATE);
        fos.write(imgBase64Str.getBytes("utf-8"));
        fos.flush();
        fos.close();

    } catch (FileNotFoundException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

}

From source file:Main.java

public static Uri getSMSLogs(ContentResolver cr, Uri internal, Context context, String timeStamp) {
    String[] smsLogArray = new String[2];
    Uri uri = Uri.parse("content://sms/inbox");
    Cursor cur = cr.query(uri, null, null, null, null);
    FileOutputStream fOut = null;

    try {// w w w . j  av  a  2s  .  co  m
        fOut = context.openFileOutput("sms_logs_" + timeStamp + ".txt", Context.MODE_PRIVATE);
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    }

    OutputStreamWriter osw = new OutputStreamWriter(fOut);

    while (cur.moveToNext()) {
        smsLogArray[0] = cur.getString(cur.getColumnIndexOrThrow("address")).toString();
        smsLogArray[1] = cur.getString(cur.getColumnIndexOrThrow("body")).toString();

        writeToOutputStreamArray(smsLogArray, osw);
    }

    try {
        osw.close();
    } catch (IOException e) {
        e.printStackTrace();
    }

    return internal;
}

From source file:it.nicola_amatucci.util.JsonAndroidLocalIO.java

public static <T> boolean saveData(Context context, String filename, T o, Class<T> obj) {
    String document = null;/*from  w ww .  j  a va 2  s .  com*/

    try {
        document = Json.json_from_object(o, obj).toString();

        if (document != null) {
            Log.i("TAG", document);
            FileOutputStream fos = context.openFileOutput(filename, Context.MODE_PRIVATE);
            fos.write(document.getBytes());
            fos.close();
            return true;
        }
    } catch (Exception e) {
        e.printStackTrace();
    }

    return false;
}