Example usage for android.graphics Bitmap compress

List of usage examples for android.graphics Bitmap compress

Introduction

In this page you can find the example usage for android.graphics Bitmap compress.

Prototype

@WorkerThread
public boolean compress(CompressFormat format, int quality, OutputStream stream) 

Source Link

Document

Write a compressed version of the bitmap to the specified outputstream.

Usage

From source file:Main.java

public static void writeBitmapFile(Context context, Bitmap bmp, String pathStr) {

    if (pathStr != null) {
        FileOutputStream fos = null;
        try {//from w  w  w .  j  ava 2  s  . co m
            fos = new FileOutputStream(pathStr);
            if (bmp != null && fos != null) {
                bmp.compress(CompressFormat.JPEG, 100, fos);
            }
            fos.close();
        } catch (Exception e) {

            e.printStackTrace();
        }
    }
}

From source file:Main.java

public static void saveBitmapForJPG(Bitmap bitmap, String file) {
    File f = new File(file);
    FileOutputStream fOut = null;
    try {/*from w  w w. j  a  va 2s.  c  o m*/
        fOut = new FileOutputStream(f);
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    }
    bitmap.compress(Bitmap.CompressFormat.JPEG, 100, fOut);
    try {
        fOut.flush();
        fOut.close();
    } catch (IOException e) {
        e.printStackTrace();
    }
}

From source file:Main.java

public static boolean save_png_from_bitmap(Bitmap bm, String filename) {

    boolean resultado = true;
    File f = new File(filename);
    if (f.exists())
        f.delete();//from  w w  w .  j av  a2 s . co  m
    try {
        FileOutputStream o_s = new FileOutputStream(f);
        bm.compress(Bitmap.CompressFormat.PNG, 100, o_s);
        o_s.flush();
        o_s.close();
    } catch (Exception e) {
        resultado = false;

        e.printStackTrace();
    }
    return resultado;
}

From source file:Main.java

public static void saveBitmap(Bitmap bitmap, String fileName) {
    long time = System.currentTimeMillis();
    String filePath = Environment.getExternalStorageDirectory() + "/" + fileName;
    try {// w  ww  .  jav  a 2s  . c om
        OutputStream stream = new FileOutputStream(filePath);
        bitmap.compress(Bitmap.CompressFormat.PNG, 80, stream);
        stream.close();
    } catch (Exception e) {
        throw new RuntimeException(e);
    }
    Log.d("artbook", "save bitmap time: " + (System.currentTimeMillis() - time));
}

From source file:Main.java

public static void saveBitmap2file(Bitmap bmp, File file) {

    OutputStream stream = null;//from   w w w .j a v  a  2s.co m
    try {
        stream = new FileOutputStream(file.getAbsolutePath());
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    }
    bmp.compress(Bitmap.CompressFormat.JPEG, 100, stream);

    try {
        stream.flush();
    } catch (IOException e) {
        e.printStackTrace();
    }

    try {
        stream.close();
    } catch (IOException e) {
        e.printStackTrace();
    }

}

From source file:Main.java

public static boolean storeBitmapToFile(Bitmap bitmap, String filePath) {
    if (Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) {
        try {//from w  ww . java  2  s  .com
            BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(filePath));
            bitmap.compress(CompressFormat.PNG, 50, bos);
            bos.flush();
            bos.close();
        } catch (Exception e) {
            return false;
        }
        return true;
    }

    return false;
}

From source file:Main.java

public static void saveBitmapToSDCard(Bitmap bitmap, String path) {
    File file = new File(path);
    if (file.exists()) {
        file.delete();//from w  ww  .  ja  v a 2 s  .com
    }
    FileOutputStream outputStream = null;
    try {
        outputStream = new FileOutputStream(file);
        bitmap.compress(Bitmap.CompressFormat.JPEG, 100, outputStream);
        outputStream.close();
    } catch (Exception e) {
        e.printStackTrace();
    }
    System.out.println("----------save success-------------------");
}

From source file:Main.java

public static byte[] compressBitmap(byte[] data, float size) {
    Bitmap bitmap = BitmapFactory.decodeByteArray(data, 0, data.length);
    if (bitmap == null || getSizeOfBitmap(bitmap) <= size) {
        return data;
    }//from  www. j  av a 2s.co  m
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    bitmap.compress(Bitmap.CompressFormat.JPEG, 100, baos);
    int quality = 100;
    while ((baos.toByteArray().length / 1024f) > size) {
        quality -= 5;
        baos.reset();
        if (quality <= 0) {
            break;
        }
        bitmap.compress(Bitmap.CompressFormat.JPEG, quality, baos);
    }
    byte[] byteData = baos.toByteArray();
    return byteData;
}

From source file:Main.java

public static boolean writeBmpToSDCard(Bitmap bmp, File file, int quality) {
    try {/*from   w  w w  . j  av  a  2 s. c  om*/
        ByteArrayOutputStream baosm = new ByteArrayOutputStream();
        if (file.getPath().toLowerCase(Locale.getDefault()).endsWith(".png")) {
            bmp.compress(Bitmap.CompressFormat.PNG, quality, baosm);
        } else {
            bmp.compress(Bitmap.CompressFormat.JPEG, quality, baosm);
        }
        byte[] bts = baosm.toByteArray();

        if (file.exists()) {
            file.delete();
        }
        file.createNewFile();

        File tempFile = new File(file.getPath() + ".png");

        FileOutputStream fosm = new FileOutputStream(tempFile);
        BufferedOutputStream bos = new BufferedOutputStream(fosm);
        bos.write(bts);
        bos.flush();
        bos.close();
        fosm.close();

        tempFile.renameTo(file);

        return true;
    } catch (Exception e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

    return false;
}

From source file:Main.java

public static boolean bitmapToOutPutStream(final Bitmap bitmap, OutputStream outputStream) {

    BufferedOutputStream out = null;
    BufferedInputStream in = null;
    try {//from  ww  w.  j  a  v  a 2s  .  co  m
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        bitmap.compress(Bitmap.CompressFormat.PNG, 100, baos);
        InputStream inputStream = new ByteArrayInputStream(baos.toByteArray());

        in = new BufferedInputStream(inputStream, 8 * 1024);
        out = new BufferedOutputStream(outputStream, 8 * 1024);

        int b;
        while ((b = in.read()) != -1) {
            out.write(b);
        }
        return true;
    } catch (IOException e) {
        e.printStackTrace();
    } finally {
        try {
            if (out != null) {
                out.close();
            }
            if (in != null) {
                in.close();
            }
        } catch (final IOException e) {
            e.printStackTrace();
        }
    }
    return false;
}