List of usage examples for android.content Context openFileOutput
public abstract FileOutputStream openFileOutput(String name, @FileMode int mode) throws FileNotFoundException;
From source file:com.kyon.klib.base.KFileUtils.java
public static void writeFile(Context context, String fileName, String writeStr) throws IOException { try {//from w w w . ja va2 s .com FileOutputStream fOut = context.openFileOutput(fileName, 0); byte[] bytes = writeStr.getBytes(); fOut.write(bytes); fOut.close(); } catch (Exception e) { e.printStackTrace(); } }
From source file:Main.java
public static void writeTofiles(Context context, Bitmap bitmap, String filename) { BufferedOutputStream outputStream = null; try {/*from ww w. j a v a 2s. co m*/ outputStream = new BufferedOutputStream(context.openFileOutput(filename, Context.MODE_PRIVATE)); bitmap.compress(Bitmap.CompressFormat.PNG, 100, outputStream); } catch (FileNotFoundException e) { e.printStackTrace(); } }
From source file:Main.java
public static void writeInputStreamToFile(InputStream is, String fileName, Context context) throws FileNotFoundException, IOException { FileOutputStream fos = null;/*from ww w . j ava 2 s.c o m*/ try { fos = context.openFileOutput(fileName, Context.MODE_PRIVATE); byte buf[] = new byte[1024]; int len = 0; while ((len = is.read(buf)) > 0) { fos.write(buf, 0, len); } fos.close(); } catch (Exception exp) { Log.v(TAG, "Failed to write to file exp:" + exp.getMessage()); exp.printStackTrace(); } }
From source file:Main.java
public static void writeByteArray(Context context, byte[] data, String fileName) { FileOutputStream outputStream; try {// w w w .jav a 2 s . co m outputStream = context.openFileOutput(fileName, Context.MODE_PRIVATE); BufferedOutputStream bos = new BufferedOutputStream(outputStream); for (byte s : data) { bos.write(s); } bos.close(); outputStream.close(); } catch (Exception e) { Log.e(TAG, e.getMessage(), e); } }
From source file:Main.java
/** * Method - saveMessage/* w w w . j a va 2s .co m*/ * * Description - Saves messages to the file by either appending or rewriting, used by any activity that needs to save the messages * * @param ctx - Context of the Application that called the function * @param message - The messages to save to the file, most often a long String of several files * @param mode - Very likely either Context.MODE_APPEND or Context.MODE_PRIVATE for appending or rewriting respectively */ public static void saveMessage(Context ctx, String message, int mode) { try { // Creating objects to write to the file FileOutputStream fos = ctx.openFileOutput("messages.dat", mode); OutputStreamWriter osw = new OutputStreamWriter(fos); osw.write(message); // Writing to the file osw.flush(); // Making sure all characters have been written // Closing objects after writing has finished osw.close(); fos.close(); } catch (FileNotFoundException e) { return; } catch (IOException e) { return; } }
From source file:Main.java
public static void writeToInternalStorage(Context context, String fileName, String json) { FileOutputStream outputStream = null; try {//from ww w . j a va 2 s.co m outputStream = context.openFileOutput(fileName, Context.MODE_PRIVATE); for (byte s : json.getBytes(StandardCharsets.UTF_8)) { outputStream.write(s); } } catch (Exception e) { Log.e(TAG, e.getMessage(), e); } finally { if (outputStream != null) { try { outputStream.close(); } catch (IOException e) { Log.e(TAG, e.getMessage(), e); } } } }
From source file:Main.java
public static boolean saveObject(Context cxt, Serializable obj, String file) { FileOutputStream fos = null;//from ww w . j ava2 s . c om ObjectOutputStream oos = null; try { fos = cxt.openFileOutput(file, Context.MODE_PRIVATE); oos = new ObjectOutputStream(fos); oos.writeObject(obj); 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 boolean writeParcelable(Context context, String fileName, Parcelable parcelObject) { boolean success = false; FileOutputStream fos = null;/*from w w w .j a va2 s. c om*/ try { fos = context.openFileOutput(fileName, Context.MODE_PRIVATE); Parcel parcel = Parcel.obtain(); parcel.writeParcelable(parcelObject, Parcelable.PARCELABLE_WRITE_RETURN_VALUE); byte[] data = parcel.marshall(); fos.write(data); success = true; } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } finally { if (fos != null) { try { fos.close(); } catch (IOException ioe) { ioe.printStackTrace(); } } } return success; }
From source file:Main.java
static void createOptOut(Context context, boolean optedOut) { FileOutputStream stream = null; try {/*from ww w. ja v a2 s . c om*/ stream = context.openFileOutput(QCMEASUREMENT_OPTOUT_STRING, Context.MODE_WORLD_WRITEABLE | Context.MODE_WORLD_READABLE); stream.write(optedOut ? 1 : 0); } catch (Exception ignored) { } finally { try { if (stream != null) { stream.close(); } } catch (IOException ignored) { } } }
From source file:Main.java
public static void write(Context context, String fileName, String content) { if (content == null) content = ""; try {/*w w w .j av a2 s . c o m*/ FileOutputStream fos = context.openFileOutput(fileName, Context.MODE_PRIVATE); fos.write(content.getBytes()); fos.close(); } catch (Exception e) { e.printStackTrace(); } }