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

public static boolean saveBitmap(Bitmap bitmap, File file) {
    if (bitmap == null)
        return false;
    FileOutputStream fos = null;
    try {//from  w w w .j  a  va 2 s  .  c  o  m
        fos = new FileOutputStream(file);
        bitmap.compress(Bitmap.CompressFormat.JPEG, 80, 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 saveBitmap(Bitmap bitmap, File file) {
    if (bitmap == null)
        return false;
    FileOutputStream fos = null;
    try {/*from ww w  .j  av  a 2s. com*/
        fos = new FileOutputStream(file);
        bitmap.compress(Bitmap.CompressFormat.PNG, 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

/**
 * //from   ww  w. j  a  v a2 s.  co  m
 * @param file
 */
public static void decodeFile(File file) {
    Bitmap bitmap = null;

    try {
        // Decode image size
        BitmapFactory.Options options = new BitmapFactory.Options();
        options.inJustDecodeBounds = true;

        FileInputStream fileInputStream = new FileInputStream(file);
        BitmapFactory.decodeStream(fileInputStream, null, options);
        fileInputStream.close();

        int scale = 1;

        if (options.outHeight > 500 || options.outWidth > 500) {
            scale = (int) Math.pow(2, (int) Math.round(
                    Math.log(500 / (double) Math.max(options.outHeight, options.outWidth)) / Math.log(0.5)));
        }

        // Decode with inSampleSize
        BitmapFactory.Options options2 = new BitmapFactory.Options();
        options2.inSampleSize = scale;

        fileInputStream = new FileInputStream(file);
        bitmap = BitmapFactory.decodeStream(fileInputStream, null, options2);
        fileInputStream.close();

        FileOutputStream output = new FileOutputStream(file);
        bitmap.compress(Bitmap.CompressFormat.JPEG, 90, output);

        output.flush();
        output.close();
    } catch (Exception e) {
        Log.e(TAG, e.getMessage());
    }
}

From source file:Main.java

public static void unpack(InputStream in, File targetDir) throws IOException {
    ZipInputStream zin = new ZipInputStream(in);
    ZipEntry entry;/*from  w w w  .  j  ava 2s.c  om*/
    while ((entry = zin.getNextEntry()) != null) {
        String extractFilePath = entry.getName();
        if ((extractFilePath.startsWith("/")) || (extractFilePath.startsWith("\\"))) {
            extractFilePath = extractFilePath.substring(1);
        }
        File extractFile = new File(new StringBuilder().append(targetDir.getPath()).append(File.separator)
                .append(extractFilePath).toString());
        if (entry.isDirectory()) {
            if (!extractFile.exists())
                extractFile.mkdirs();
        } else {
            File parent = extractFile.getParentFile();
            if (!parent.exists()) {
                parent.mkdirs();
            }

            FileOutputStream os = new FileOutputStream(extractFile);
            copyFile(zin, os);
            os.flush();
            os.close();
        }
    }
}

From source file:Main.java

public static boolean saveBitmap(Bitmap bitmap, File file, int quality) {
    if (bitmap == null)
        return false;
    FileOutputStream fos = null;
    try {/*from   w  w w  .j  a  va  2  s  . c  o m*/
        fos = new FileOutputStream(file);
        bitmap.compress(Bitmap.CompressFormat.JPEG, quality, 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 String writeBitmap(byte[] data, int cameraDegree, Rect rect, Rect willTransformRect)
        throws IOException {
    File file = new File(Environment.getExternalStorageDirectory() + "/bookclip/");
    file.mkdir();/*from  w w  w . ja v  a 2s.c o  m*/
    String bitmapPath = file.getPath() + "/" + System.currentTimeMillis() + ".png";

    // bitmap rotation, scaling, crop
    BitmapFactory.Options option = new BitmapFactory.Options();
    option.inSampleSize = 2;
    Bitmap bitmap = BitmapFactory.decodeByteArray(data, 0, data.length, option);
    Matrix bitmapMatrix = new Matrix();
    bitmapMatrix.setRotate(cameraDegree);
    int x = rect.left, y = rect.top, width = rect.right - rect.left, height = rect.bottom - rect.top;
    Bitmap rotateBitmap = Bitmap.createBitmap(bitmap, 0, 0, bitmap.getWidth(), bitmap.getHeight(), bitmapMatrix,
            false);
    // bitmap recycle
    bitmap.recycle();

    Bitmap scaledBitmap = Bitmap.createScaledBitmap(rotateBitmap, willTransformRect.right,
            willTransformRect.bottom - willTransformRect.top, false);
    // rotatebitmap recycle
    rotateBitmap.recycle();

    Bitmap cropBitmap = Bitmap.createBitmap(scaledBitmap, x, y, width, height, null, false);
    // scaledBitmap recycle
    scaledBitmap.recycle();

    // file write
    FileOutputStream fos = new FileOutputStream(new File(bitmapPath));
    cropBitmap.compress(CompressFormat.PNG, 100, fos);
    fos.flush();
    fos.close();

    // recycle
    cropBitmap.recycle();

    return bitmapPath;
}

From source file:Main.java

public static boolean saveBitmap(Bitmap bitmap, File file) {
    if (bitmap == null)
        return false;
    FileOutputStream fos = null;
    try {// w  w w.j  ava2 s.  c  o  m
        fos = new FileOutputStream(file, false);
        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

/**
 * Creates a copy of the file, ensuring the file is written to the disk
 * @param in Source file/*from   www . j  a  va 2s  .  c om*/
 * @param out Destination file
 * @throws IOException if the operation fails
 */
public static void copyFileSync(File in, File out) throws IOException {
    FileInputStream inStream = new FileInputStream(in);
    FileOutputStream outStream = new FileOutputStream(out);
    try {
        copyStream(inStream, outStream);
    } finally {
        inStream.close();
        outStream.flush();
        outStream.getFD().sync();
        outStream.close();
    }
}

From source file:Main.java

private static boolean tryToSaveBitmap(Bitmap bitmap, String savePath) throws IOException {
    FileOutputStream output = new FileOutputStream(savePath);
    try {//from   w w w  . j  a v  a2  s  .c  om
        return bitmap.compress(Bitmap.CompressFormat.PNG, 100, output);
    } finally {
        try {
            output.flush();
            output.close();
        } catch (Exception e) {
            // ignore...
        }
    }
}

From source file:Main.java

public static void setFBImage(final String fbid, final Context context, final ImageView v) {
    new AsyncTask<String, Void, Bitmap>() {

        @Override/* w ww .  ja v a 2  s . c o  m*/
        protected Bitmap doInBackground(String... strings) {
            File img = new File(context.getFilesDir() + "/profile.jpg");
            Bitmap bmp = null;
            if (img.exists()) {
                try {
                    bmp = BitmapFactory.decodeFile(img.getAbsolutePath());
                } catch (Exception E) {
                    E.printStackTrace();
                }
            } else {
                try {
                    URL img_url = new URL("https://graph.facebook.com/" + String.valueOf(fbid)
                            + "/picture?type=large&redirect=true&width=400&height=400");
                    bmp = BitmapFactory.decodeStream(img_url.openConnection().getInputStream());
                    FileOutputStream fOut = new FileOutputStream(img);
                    bmp.compress(Bitmap.CompressFormat.JPEG, 90, fOut);
                    fOut.flush();
                    fOut.close();
                } catch (Exception E) {
                    E.printStackTrace();
                }
            }
            return bmp;
        }

        @Override
        protected void onPostExecute(Bitmap img) {
            if (img != null) {
                v.setImageBitmap(img);
            }
        }
    }.execute();
}