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 saveToFile(Bitmap bmp, String filePath) throws IOException {
    File file = new File(filePath);
    if (!file.exists()) {
        file.createNewFile();/*from   w w  w. j  a v  a2s.c om*/
    }
    FileOutputStream fout = new FileOutputStream(file);
    bmp.compress(Bitmap.CompressFormat.JPEG, 100, fout);
    fout.close();
}

From source file:Main.java

public static boolean saveBitmap(Bitmap bitmap, File file, int quality) {
    if (bitmap == null)
        return false;
    FileOutputStream fos = null;/*from  w ww.  java 2  s  . c  o  m*/
    try {
        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 boolean saveImage(Bitmap src, String filepath, CompressFormat format) {
    boolean rs = false;
    File file = new File(filepath);
    try {//  w  w w  .j  a  v a 2 s . com
        FileOutputStream out = new FileOutputStream(file);
        if (src.compress(format, 100, out)) {
            out.flush();
        }
        out.close();
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }
    return rs;
}

From source file:Main.java

public static boolean putBitmapToFile(Bitmap bitmap, File file) {
    if (bitmap == null || file == null)
        return false;
    FileOutputStream fos = null;//from  ww w .ja  v  a 2 s .  c  o  m
    try {
        fos = new FileOutputStream(file);
        bitmap.compress(Bitmap.CompressFormat.PNG, 100, fos);
        return true;
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    } finally {
        if (fos != null)
            try {
                fos.flush();
                fos.close();
                if (!bitmap.isRecycled())
                    bitmap.recycle();
            } catch (IOException ignored) {
            }
    }
    return false;
}

From source file:Main.java

public static void storeImage(Bitmap bitmap, String outPath) throws FileNotFoundException {
    File file = new File(outPath);
    if (file.exists()) {
        file.delete();/*w w w .  ja  va 2s.co m*/
    }
    FileOutputStream os = new FileOutputStream(outPath);
    bitmap.compress(Bitmap.CompressFormat.JPEG, 100, os);
}

From source file:Main.java

private static void saveBitmap(Bitmap bitmap, String fileName) {
    File myCaptureFile = new File(fileName);
    BufferedOutputStream bos;/*from w w  w  .  j  av  a2  s .com*/
    try {
        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

/**
 * Create a byte array out of drawable/*from   w  w w.j av a2  s.  co m*/
 *
 * @param drawable The source drawable
 * @return The byte array of source drawable
 */
public static byte[] getByteArrayFromDrawable(Drawable drawable) {
    if (drawable == null) {
        return null;
    }

    Bitmap bitmap = getBitmapFromDrawable(drawable);
    if (bitmap == null) {
        return null;
    }
    ByteArrayOutputStream stream = new ByteArrayOutputStream();
    bitmap.compress(Bitmap.CompressFormat.PNG, 100, stream);
    return stream.toByteArray();
}

From source file:Main.java

public static int getByteCount(Bitmap bitmap, Bitmap.CompressFormat mCompressFormat) {
    int size = 0;
    ByteArrayOutputStream output = null;
    try {/*  www  .  jav a  2s  .  co m*/
        output = new ByteArrayOutputStream();
        bitmap.compress(mCompressFormat, 100, output);
        byte[] result = output.toByteArray();
        size = result.length;
        result = null;
    } catch (Exception e) {
        e.printStackTrace();
    } finally {
        if (output != null) {
            try {
                output.close();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }
    return size;
}

From source file:Main.java

public static boolean saveFile(Bitmap bitmap, String filename) {
    File sd = Environment.getExternalStorageDirectory();
    File dest = new File(sd, filename);
    try {/*www. jav  a2  s  . co  m*/
        FileOutputStream out = new FileOutputStream(dest);
        bitmap.compress(Bitmap.CompressFormat.PNG, 100, out);
        out.flush();
        out.close();
    } catch (Exception e) {
        e.printStackTrace();
        return false;
    }
    return true;
}

From source file:Main.java

public static void saveImage(String path, Bitmap bitmap) throws IOException {
    File file = new File(path);
    if (!file.getParentFile().exists()) {
        file.getParentFile().mkdirs();//from www .j  av a 2 s  .  c  om
    }
    FileOutputStream fos = new FileOutputStream(file);
    bitmap.compress(Bitmap.CompressFormat.JPEG, 100, fos);
    fos.flush();
    fos.close();
}