FileOutputStream out = null; try { out = new FileOutputStream(filename); bmp.compress(Bitmap.CompressFormat.PNG, 100, out); // bmp is your Bitmap instance // PNG is a lossless format, the compression factor (100) is ignored } catch (Exception e) { e.printStackTrace(); } finally { try { if (out != null) { out.close(); } } catch (IOException e) { e.printStackTrace(); } }
FileOutputStream fos = new FileOutputStream("pathname"); fos.write(myByteArray); fos.close();
//create a file to write bitmap data File f = new File(context.getCacheDir(), filename); f.createNewFile(); //Convert bitmap to byte array Bitmap bitmap = your bitmap; ByteArrayOutputStream bos = new ByteArrayOutputStream(); bitmap.compress(CompressFormat.PNG, 0 /*ignored for PNG*/, bos); byte[] bitmapdata = bos.toByteArray(); //write the bytes in file FileOutputStream fos = new FileOutputStream(f); fos.write(bitmapdata); fos.flush(); fos.close();
public void copy(File src, File dst) throws IOException { FileInputStream inStream = new FileInputStream(src); FileOutputStream outStream = new FileOutputStream(dst); FileChannel inChannel = inStream.getChannel(); FileChannel outChannel = outStream.getChannel(); inChannel.transferTo(0, inChannel.size(), outChannel); inStream.close(); outStream.close(); }
private Object test(Object obj) { File file = new File("r"); FileOutputStream output = null; try { output = new FileOutputStream(file); if (obj.equals("a")) { return new Object(); } else { return null; } } catch (IOException e) { System.out.println("Exception"); return null; } finally { if (output != null) { try { output.close(); } catch (IOException e) { // Ignored } } file.delete(); } } }
/** * Cache an element. * * @param bitmap The bitmap to be put in the cache. * @param fileName A string representing the name of the file to be cached. */ synchronized void put(Bitmap bitmap, String fileName) { final File file = buildFileFromFilename(fileName); if (!file.exists()) { try { final FileOutputStream fileOutputStream = new FileOutputStream(file); bitmap.compress(Bitmap.CompressFormat.PNG, 90, fileOutputStream); fileOutputStream.flush(); fileOutputStream.close(); } catch (IOException e) { Log.e(TAG, e.getMessage()); } } }
boolean success = false; // Encode the file as a PNG image. FileOutputStream outStream; try { outStream = new FileOutputStream(image); bitmap.compress(Bitmap.CompressFormat.PNG, 100, outStream); /* 100 to keep full quality of the image */ outStream.flush(); outStream.close(); success = true; } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); }
/** * Implements the same behaviour as the "touch" utility on Unix. It creates * a new file with size 0 or, if the file exists already, it is opened and * closed without modifying it, but updating the file date and time. * <p> * NOTE: As from v1.3, this method throws an IOException if the last * modified date of the file cannot be set. Also, as from v1.3 this method * creates parent directories if they do not exist. * * @param file the File to touch * @throws IOException If an I/O problem occurs */ public static void touch(final File file) throws IOException { if (!file.exists()) { openOutputStream(file).close(); } final boolean success = file.setLastModified(System.currentTimeMillis()); if (!success) { throw new IOException("Unable to set the last modification time for " + file); } }
ByteArrayOutputStream bytes = new ByteArrayOutputStream(); _bitmapScaled.compress(Bitmap.CompressFormat.JPEG, 40, bytes); //you can create a new file name "test.jpg" in sdcard folder. File f = new File(Environment.getExternalStorageDirectory() + File.separator + "test.jpg"); f.createNewFile(); //write the bytes in file FileOutputStream fo = new FileOutputStream(f); fo.write(bytes.toByteArray()); // remember close de FileOutput fo.close();
FileInputStream in = new FileInputStream("First.properties"); Properties props = new Properties(); props.load(in); in.close(); FileOutputStream out = new FileOutputStream("First.properties"); props.setProperty("country", "america"); props.store(out, null); out.close();
public static void writeFile(byte[] data, File out) throws IOException { FileOutputStream fos = new FileOutputStream(out); fos.write(data); fos.close(); }
private Revision createNewFileAndCheckIn(File directory) { try { new FileOutputStream(new File(directory, "test.txt")).close(); addremove(directory); commit("created test.txt", directory); } catch (IOException e) { e.printStackTrace(); } return latestRevisionOf(); }
private static void writeUriContentToTempFileIfNotExists(Context context, Uri uri, File tempFile) throws IOException { synchronized (tempFileWriteMonitor) { if (tempFile.exists()) { return; } FileOutputStream outputStream = new FileOutputStream(tempFile); InputStream inputStream = context.getContentResolver().openInputStream(uri); if (inputStream == null) { throw new IOException("Failed to resolve content at uri: " + uri); } IOUtils.copy(inputStream, outputStream); outputStream.close(); IOUtils.closeQuietly(inputStream); } }
String strMyImagePath = f.getAbsolutePath(); FileOutputStream fos = null; try { fos = new FileOutputStream(f); bitmap.compress(Bitmap.CompressFormat.PNG,70, fos); fos.flush(); fos.close(); // MediaStore.Images.Media.insertImage(getContentResolver(), b, "Screen", "screen"); }catch (FileNotFoundException e) { e.printStackTrace(); } catch (Exception e) { e.printStackTrace(); }
public static void savePreferences() throws IOException { FileOutputStream fos = new FileOutputStream(getPreferencesFile()); try { getPreferences().store(fos, ""); } finally { fos.close(); } }
String file_path = Environment.getExternalStorageDirectory().getAbsolutePath() + "/PhysicsSketchpad"; File dir = new File(file_path); if(!dir.exists()) dir.mkdirs(); File file = new File(dir, "sketchpad" + pad.t_id + ".png"); FileOutputStream fOut = new FileOutputStream(file); bmp.compress(Bitmap.CompressFormat.PNG, 85, fOut); fOut.flush(); fOut.close();
/** * Copy a file from one place to another */ public static void copyFile(File in, File out) throws IOException { FileInputStream fis = new FileInputStream(in); FileOutputStream fos = new FileOutputStream(out); try { copyStream(fis, fos); } finally { fis.close(); fos.close(); } }
FileOutputStream stream = new FileOutputStream(path); try { stream.write(bytes); } finally { stream.close(); }
protected static void touchFile(File file) throws IOException { if (!file.exists()) { new FileOutputStream(file).close(); } if (!file.setLastModified(System.currentTimeMillis())) { throw new IOException("Could not touch the file."); } }