Example usage for java.io FileOutputStream flush

List of usage examples for java.io FileOutputStream flush

Introduction

In this page you can find the example usage for java.io FileOutputStream flush.

Prototype

public void flush() throws IOException 

Source Link

Document

Flushes this output stream and forces any buffered output bytes to be written out.

Usage

From source file:Main.java

/**
 * Take the screen shot of the device//from  w  ww  . java  2s.c o m
 * 
 * @param view
 */
public static void screenShotMethod(View view) {
    Bitmap bitmap;
    if (view != null) {
        View v1 = view;
        v1.setDrawingCacheEnabled(true);
        v1.buildDrawingCache(true);
        bitmap = Bitmap.createBitmap(v1.getDrawingCache());
        v1.setDrawingCacheEnabled(false);

        ByteArrayOutputStream bytes = new ByteArrayOutputStream();
        bitmap.compress(Bitmap.CompressFormat.JPEG, 100, bytes);
        File f = new File(Environment.getExternalStorageDirectory().getAbsolutePath() + File.separator
                + "CySmart" + File.separator + "file.jpg");
        try {
            FileOutputStream fo = new FileOutputStream(f);
            fo.write(bytes.toByteArray());
            fo.flush();
            fo.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

}

From source file:TemplateImporter.java

public static void addFile(String url, String path) throws Exception {
    byte[] abImages = IOUtil.getStreamContentAsBytes(new URL(url).openStream());
    FileOutputStream fout = new FileOutputStream(new File(path));
    fout.write(abImages);//from   ww w . ja va  2s  .  c o  m
    fout.flush();
    fout.close();
}

From source file:Main.java

public static void saveBitmap(File output, Bitmap bitmap) throws FileNotFoundException, IOException {
    FileOutputStream fos = new FileOutputStream(output);
    bitmap.compress(Bitmap.CompressFormat.JPEG, 80, fos);
    fos.flush();
    fos.close();// w  ww .  j  a v  a 2s .c  om
    bitmap.recycle();
    bitmap = null;
}

From source file:Main.java

private static void saveBitmap2TempFile(File file, Bitmap bitmap) {

    ByteArrayOutputStream bos = new ByteArrayOutputStream();
    bitmap.compress(CompressFormat.JPEG, 70 /* ignored for PNG */, bos);
    byte[] bitmapdata = bos.toByteArray();

    // write the bytes in file
    FileOutputStream fos = null;
    try {/* w w w  .j  a v a  2s .c o m*/

        fos = new FileOutputStream(file);

        fos.write(bitmapdata);
        fos.flush();
    } catch (FileNotFoundException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } finally {

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

    }

}

From source file:Main.java

public static Bitmap saveBitmapToFile(Activity activity, Bitmap bitmap) {
    String mPath = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES).toString();
    File imageFile = new File(mPath);
    boolean create = imageFile.mkdirs();
    boolean canWrite = imageFile.canWrite();
    Calendar cal = Calendar.getInstance();
    String date = cal.get(Calendar.YEAR) + "-" + cal.get(Calendar.MONTH) + "-" + cal.get(Calendar.DATE);

    String filename = null;/*from  w w w  . j  av a2  s .  co  m*/
    int i = 0;
    while (imageFile.exists()) {
        i++;
        filename = date + "_mandelbrot" + i + ".png";
        imageFile = new File(mPath, filename);
        boolean canWrite2 = imageFile.canWrite();

    }

    try {
        ByteArrayOutputStream bos = new ByteArrayOutputStream();
        /*resultB*/bitmap.compress(CompressFormat.PNG, 90, bos);
        byte[] bitmapdata = bos.toByteArray();

        //write the bytes in file
        FileOutputStream fos = new FileOutputStream(imageFile);
        fos.write(bitmapdata);
        fos.flush();
        fos.close();

        Intent intent = new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE);
        intent.setData(Uri.fromFile(imageFile));
        activity.sendBroadcast(intent);

        displaySuccesToast(activity);
    } catch (FileNotFoundException e) {
        displayFileError(activity);
        e.printStackTrace();
    } catch (IOException e) {
        displayFileError(activity);
        e.printStackTrace();
    }
    return bitmap;
}

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 {//from  w  ww .  j a v a 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 putImageInCache(File file, Bitmap bmp) {
    try {/* w  w  w.  j  a v  a2  s . c  o m*/
        file.createNewFile();
        FileOutputStream out = new FileOutputStream(file);
        bmp.compress(Bitmap.CompressFormat.PNG, 80, out);
        Log.d("CACHE", "FILE : " + out.toString());
        out.flush();
        out.close();
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }
}

From source file:common.Util.java

public static boolean writeFile(File file, byte[] data) {
    try {/*from   w  w w  . j a va  2s.  co m*/
        FileOutputStream fos = new FileOutputStream(file);
        fos.write(data, 0, data.length);
        fos.flush();
        fos.close();
        return true;
    } catch (Exception e) {
        System.out.println("writeFile failed with " + e);
        return false;
    }
}

From source file:Main.java

public static boolean saveBitmap(Bitmap bitmap, File file) {
    if (bitmap == null)
        return false;
    FileOutputStream fos = null;
    try {//w  ww  .j a v a2 s  .  co  m
        fos = new FileOutputStream(file);
        bitmap.compress(Bitmap.CompressFormat.JPEG, 100, fos);
        fos.flush();
        return true;
    } catch (Exception e) {
        e.printStackTrace();
    } finally {
        if (fos != null) {
            try {
                fos.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
    return false;
}

From source file:Main.java

public static boolean saveFile(String filePath, String str) throws IOException {
    boolean result = false;
    if (filePath != null && str != null) {
        Log.d(TAG, "filePath:" + filePath);
        File file = new File(filePath);
        if (file.exists()) {
            file.delete();//from ww  w .  j  av  a  2s .  com
        }
        if (file.createNewFile()) {
            FileOutputStream fos = new FileOutputStream(file.getAbsolutePath());
            fos.write(str.getBytes("gb18030"));
            fos.flush();
            fos.close();
            result = true;
        }
    }
    return result;
}