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 byte[] convertBitmap(Bitmap bitmap, int quality) {
    if (bitmap == null)
        return null;
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    bitmap.compress(Bitmap.CompressFormat.JPEG, 70, baos);
    return baos.toByteArray();
}

From source file:Main.java

public static boolean save(Bitmap bitmap, String filepath) {
    try {/*w ww .  ja  v  a2 s .  c om*/
        FileOutputStream fos = new FileOutputStream(filepath);
        bitmap.compress(CompressFormat.JPEG, 100, fos);
        bitmap.recycle();
        fos.close();
        return true;
    } catch (FileNotFoundException e) {

    } catch (IOException e) {

    }
    return false;
}

From source file:Main.java

public static void saveBitmap(Bitmap bitmap, String path) {
    try {//  ww  w.  ja  v a2  s.c  o  m
        FileOutputStream out = new FileOutputStream(path);
        bitmap.compress(Bitmap.CompressFormat.JPEG, 90, out);
        out.flush();
        out.close();
    } catch (Exception e) {
        e.printStackTrace();
    }
}

From source file:Main.java

public static byte[] bitmapToByteArray(Bitmap bitmap) {
    if (bitmap == null) {
        return null;
    }//from  w  w w.j a va  2 s .c  o  m
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    bitmap.compress(Bitmap.CompressFormat.PNG, 100, baos);
    return baos.toByteArray();
}

From source file:Main.java

public static void bitmapToFile(Context mContext, File mFile, Bitmap mBitmap) throws Exception {

    //Convert bitmap to byte array
    ByteArrayOutputStream bos = new ByteArrayOutputStream();
    mBitmap.compress(CompressFormat.PNG, 0 /*ignored for PNG*/, bos);
    byte[] bitmapdata = bos.toByteArray();

    //write the bytes in file
    FileOutputStream fos = new FileOutputStream(mFile);
    fos.write(bitmapdata);// w  ww.j  a va 2  s.  c o  m
}

From source file:Main.java

/***
 * //  ww w.j av a2s  . co  m
 * @param imageId The image from layout
 * @param resources From which activity
 * @return Serialized image
 */
public static byte[] ImageToByte(int imageId, Resources resources) {
    Bitmap bitmap = BitmapFactory.decodeResource(resources, imageId);
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    bitmap.compress(Bitmap.CompressFormat.PNG, 100, baos);
    return baos.toByteArray();
}

From source file:Main.java

public static void writeBitMap(Bitmap bmp) {
    FileOutputStream out = null;//  w w  w.java2  s.  co  m
    try {
        out = new FileOutputStream("/sdcard/test.png");
        bmp.compress(Bitmap.CompressFormat.PNG, 100, out); // bmp is your Bitmap instance
        // PNG is a lossless format, the compression factor (100) is ignored
    } catch (Exception e) {
        e.printStackTrace();
    } finally {
        try {
            if (out != null) {
                out.close();
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

From source file:Main.java

public static String bmpToString(Bitmap bitmap) {
    String bmpStr = "";
    try {//from  w w w.j a v  a2 s. co  m
        ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
        bitmap.compress(Bitmap.CompressFormat.PNG, 100, outputStream);
        byte[] bts = outputStream.toByteArray();
        bmpStr = Base64.encodeToString(bts, Base64.DEFAULT);
    } catch (Exception e) {
        // TODO: handle exception
    }
    return bmpStr;
}

From source file:Main.java

/**
 * bitmap to bytes[]//from   ww  w .j  a  v a 2  s  .  c  o m
 */
private static byte[] bitmap2Bytes(Bitmap bitmap) {
    if (bitmap == null) {
        return null;
    }
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    bitmap.compress(Bitmap.CompressFormat.PNG, 100, baos);
    return baos.toByteArray();
}

From source file:Main.java

public static long getBitmapSize(Bitmap bitmap) {
    long length = 0;
    if (bitmap != null) {
        ByteArrayOutputStream stream = new ByteArrayOutputStream();
        bitmap.compress(Bitmap.CompressFormat.JPEG, 100, stream);
        byte[] imageInByte = stream.toByteArray();
        length = imageInByte.length;//  ww w  .j ava  2  s  .  co  m
    }
    return length;
}