Example usage for java.io BufferedOutputStream flush

List of usage examples for java.io BufferedOutputStream flush

Introduction

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

Prototype

@Override
public synchronized void flush() throws IOException 

Source Link

Document

Flushes this buffered output stream.

Usage

From source file:Main.java

public static void saveImageToSD(Context ctx, String filePath, Bitmap bitmap, int quality) throws IOException {
    if (bitmap != null) {
        File file = new File(filePath.substring(0, filePath.lastIndexOf(File.separator)));
        if (!file.exists()) {
            file.mkdirs();/*from w  w w .  java2 s.  co m*/
        }
        file = new File(filePath);
        if (!file.exists()) {
            file.createNewFile();
        }
        BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(filePath));
        bitmap.compress(CompressFormat.JPEG, quality, bos);
        bos.flush();
        bos.close();
        if (ctx != null) {
            scanPhoto(ctx, filePath);
        }
    }
}

From source file:Main.java

public static boolean StoreByteImage(Context mContext, byte[] imageData, int quality, String expName) {

    File sdImageMainDirectory = new File("/sdcard/myImages");
    FileOutputStream fileOutputStream = null;
    String nameFile = "";
    try {//  www  .  ja va 2 s. com

        BitmapFactory.Options options = new BitmapFactory.Options();
        options.inSampleSize = 5;

        Bitmap myImage = BitmapFactory.decodeByteArray(imageData, 0, imageData.length, options);

        fileOutputStream = new FileOutputStream(sdImageMainDirectory.toString() + "/" + nameFile + ".jpg");

        BufferedOutputStream bos = new BufferedOutputStream(fileOutputStream);

        myImage.compress(CompressFormat.JPEG, quality, bos);

        bos.flush();
        bos.close();

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

    return true;
}

From source file:Main.java

public static Bitmap getAndSetBitmapFromNet(String urlPath) {

    Bitmap bm = null;//from   w w  w.  j  a v  a2s  . c  o m

    if (urlPath != null) {
        try {
            BufferedInputStream bis = new BufferedInputStream(new URL(urlPath).openStream(), 1024);
            final ByteArrayOutputStream dataStream = new ByteArrayOutputStream();
            BufferedOutputStream out = new BufferedOutputStream(dataStream, 1024);
            copy(bis, out);
            out.flush();
            final byte[] data = dataStream.toByteArray();
            bm = BitmapFactory.decodeByteArray(data, 0, data.length);
            Log.i(I, "data.length: " + data.length);
            out.close();
            dataStream.close();
            bis.close();
            bm = processBitmap(bm);
        } catch (IOException e) {
            Log.i(I, "URL Connection or Bitmap processing Exception");
            e.printStackTrace();
        }
    }
    return bm;
}

From source file:Main.java

private static void saveBitmap(Bitmap bitmap, String fileName) {
    File myCaptureFile = new File(fileName);
    BufferedOutputStream bos;
    try {//from w  w w  .j  a va  2 s .  com
        bos = new BufferedOutputStream(new FileOutputStream(myCaptureFile));
        bitmap.compress(Bitmap.CompressFormat.PNG, 80, bos);
        bos.flush();
        bos.close();
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }
}

From source file:Main.java

public static boolean saveFile(Bitmap bm, String path) {
    if (bm == null || path == null)
        return false;
    File myCaptureFile = new File(path);
    if (myCaptureFile.exists()) {
        myCaptureFile.delete();//from  www.j a  va 2 s  .  co  m
    }
    try {
        BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(myCaptureFile));
        bm.compress(Bitmap.CompressFormat.JPEG, 80, bos);
        bos.flush();
        bos.close();
        return true;
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }
    return false;
}

From source file:Main.java

public static boolean saveBitmapToSDCard(Bitmap bitmap, String filePath, String fileName) {
    boolean flag = false;
    if (null != bitmap) {
        try {//from ww w .  j  av a  2  s . co  m
            fileName = fileName + ".jpg";
            File file = new File(filePath);
            if (!file.exists()) {
                file.mkdirs();
            }
            File f = new File(filePath + fileName);
            if (f.exists()) {
                f.delete();
            }
            BufferedOutputStream outputStream = new BufferedOutputStream(new FileOutputStream(f));
            bitmap.compress(CompressFormat.JPEG, 100, outputStream);
            outputStream.flush();
            outputStream.close();
            flag = true;
        } catch (FileNotFoundException e) {
            flag = false;
        } catch (IOException e) {
            flag = false;
        }
    }
    return flag;

}

From source file:Main.java

/**
 * author: liuxu//from  w  w w. j a v  a 2 s .  co  m
 * save  bitmap into file
 * @param bitmap the bitmap
 * @param path full path of the file
 * @param quality  Hint to the compressor, 0-100. 0 meaning compress for
 *                 small size, 100 meaning compress for max quality. Some
 *                 formats, like PNG which is lossless, will ignore the
 *                 quality setting
 * @return true if success
 */
public static boolean saveBitmap(Bitmap bitmap, String path, int quality) {
    if (bitmap == null) {
        return false;
    }
    File file = new File(path);
    File parent = file.getParentFile();
    if (parent != null && !parent.exists()) {
        if (!parent.mkdirs()) {
            //Log.e(TAG, "saveBitmap, mkdir for parent fail");
            return false;
        }
    }
    try {
        BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(file));
        bitmap.compress(Bitmap.CompressFormat.JPEG, quality, bos);
        bos.flush();
        bos.close();
    } catch (IOException e) {
        //Log.d(TAG, "saveBitmap fail", e);
        return false;
    }
    return true;
}

From source file:Main.java

public static int copyFile(String fileDir, String fileName, byte[] buffer) {
    if (buffer == null) {
        return -2;
    }/*from   w ww  .  j av a  2  s. co m*/

    try {
        File file = new File(fileDir);
        if (!file.exists()) {
            file.mkdirs();
        }
        File resultFile = new File(file, fileName);
        if (!resultFile.exists()) {
            resultFile.createNewFile();
        }
        BufferedOutputStream bufferedOutputStream = new BufferedOutputStream(
                new FileOutputStream(resultFile, true));
        bufferedOutputStream.write(buffer);
        bufferedOutputStream.flush();
        bufferedOutputStream.close();
        return 0;

    } catch (Exception e) {
    }
    return -1;
}

From source file:Main.java

public static void copyToMemory(Context context, String srcFilePath, String dictFileName) throws IOException {
    File srcFile = new File(srcFilePath);
    if (!srcFile.exists() || srcFile.isDirectory()) {
        return;//from  ww w . j  a  v a2s  . com
    }
    BufferedInputStream inBufferedInputStream = new BufferedInputStream(new FileInputStream(srcFile));
    FileOutputStream fos = context.openFileOutput(dictFileName, 0);
    BufferedOutputStream bos = new BufferedOutputStream(fos);
    byte[] b = new byte[1024 * 4];
    int len;
    while ((len = inBufferedInputStream.read(b)) != -1) {
        bos.write(b, 0, len);
        bos.flush();
    }
    inBufferedInputStream.close();
    bos.close();
}

From source file:nl.clockwork.common.util.Utils.java

public static byte[] unzip(byte[] content) throws IOException {
    int BUFFER_SIZE = 2048;
    ByteArrayOutputStream out = new ByteArrayOutputStream();
    ZipInputStream zin = new ZipInputStream(new ByteArrayInputStream(content));
    ZipEntry entry = zin.getNextEntry();
    if (entry != null) {
        int count;
        byte buffer[] = new byte[BUFFER_SIZE];
        BufferedOutputStream bout = new BufferedOutputStream(out, BUFFER_SIZE);
        while ((count = zin.read(buffer, 0, BUFFER_SIZE)) != -1)
            bout.write(buffer, 0, count);
        bout.flush();
        bout.close();// www  .  j  a  va  2 s .c  o m
        zin.close();
    }
    return out.toByteArray();
}