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 boolean writeBitmapToFile(Bitmap bitmap, String path) {
    FileOutputStream fos = null;// w  ww .ja  v  a 2s  .c  o  m
    try {
        fos = new FileOutputStream(path);
        bitmap.compress(Bitmap.CompressFormat.PNG, 100, fos);
        return true;
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    } finally {
        if (fos != null)
            try {
                fos.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
    }
    return false;
}

From source file:Main.java

/**
 * Write image to internal storage//from   w w  w  .  j  av  a2s .c o m
 * @param context  : Context android.
 * @param fileName : Image file name.
 * @param image    : Bitmap image format PNG only.
 */
public static void saveImageToInternalStorage(Context context, String fileName, Bitmap image) {
    // Convert Bitmap to byteArray
    ByteArrayOutputStream stream = new ByteArrayOutputStream();
    image.compress(Bitmap.CompressFormat.PNG, 100, stream);
    byte[] byteArray = stream.toByteArray();

    // Open fileOutput with fileName and write byteArray
    try {
        FileOutputStream fos = context.openFileOutput(fileName, Context.MODE_PRIVATE);
        if (fos != null) {
            fos.write(byteArray);
            fos.close();
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
}

From source file:Main.java

public static byte[] ImageToByte(ImageView image) {
    Bitmap bitmap = ((BitmapDrawable) image.getDrawable()).getBitmap();
    Bitmap resized = Bitmap.createScaledBitmap(bitmap, 150, 150, true);
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    resized.compress(Bitmap.CompressFormat.PNG, 100, baos);

    return baos.toByteArray();
}

From source file:Main.java

/**
 * @brief      Converting bitmap to bytes array.
 * //w w w .j  a  v a 2s  .c o  m
 * @param      bm   [IN]  bitmap object
 * 
 * @return     Return converted bytes array
 */
public static byte[] bitmapToBytes(Bitmap bm) {
    if (bm == null) {
        return null;
    }

    final ByteArrayOutputStream os = new ByteArrayOutputStream();
    bm.compress(Bitmap.CompressFormat.PNG, 100, os);
    return os.toByteArray();
}

From source file:Main.java

/**
 * @author Casper Rasmussen - 2012/*ww  w  .j av  a 2s. c  om*/
 * Use this method to get byte[] from a file of a given path.
 * @param filePath
 * @param compressRate - 0-100%, 100% means no compression
 * @param mCompressFormat - CompressFormat.PNG, CompressFormat.JPEG etc.
 * @return byte[]
 */
public static byte[] filePathToByteArray(String filePath, int compressRate, CompressFormat mCompressFormat) {
    BitmapFactory.Options options = new BitmapFactory.Options();
    options.inJustDecodeBounds = false;
    Bitmap bitmapFromFile = BitmapFactory.decodeFile(filePath, options);

    ByteArrayOutputStream stream = new ByteArrayOutputStream();
    bitmapFromFile.compress(mCompressFormat, compressRate, stream);
    byte[] byteArray = stream.toByteArray();

    return byteArray;
}

From source file:Main.java

private static boolean tryToSaveBitmap(Bitmap bitmap, String savePath) throws IOException {
    FileOutputStream output = new FileOutputStream(savePath);
    try {//from  www . jav a2 s. co m
        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 byte[] Bitmap2Bytes(Bitmap bm) {
    if (bm == null) {
        return null;
    }/*  ww w  . j  a  v a 2 s. c o m*/
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    bm.compress(CompressFormat.PNG, 100, baos);

    if (bm != null) {
        bm.recycle();
        bm = null;
    }

    return baos.toByteArray();
}

From source file:Main.java

public static void compressWithQuality(Bitmap image, String outPath, int maxSize) throws IOException {

    ByteArrayOutputStream os = new ByteArrayOutputStream();

    image.compress(Bitmap.CompressFormat.JPEG, 20, os);

    while (os.toByteArray().length / 1024 > 1024) {
        os.reset();/* w w w .ja  v a 2s.  c  o m*/
        image.compress(Bitmap.CompressFormat.JPEG, 20, os);
    }

    ByteArrayInputStream bi = new ByteArrayInputStream(os.toByteArray());
    BitmapFactory.decodeStream(bi);
    FileOutputStream fi = new FileOutputStream(outPath);
    fi.write(os.toByteArray());
    fi.flush();
    fi.close();

}

From source file:Main.java

public static byte[] bitmapToBytes(Bitmap bitmap) {
    if (bitmap == null) {
        return null;

    }//from   w  w w  . jav a  2  s.co  m
    final ByteArrayOutputStream os = new ByteArrayOutputStream();
    bitmap.compress(Bitmap.CompressFormat.PNG, 100, os);
    return os.toByteArray();

}

From source file:Main.java

public static void saveBitmapToFile(Bitmap bitmap, File file) {

    try {//from  w w  w. j  a  v a  2s  . co m
        BufferedOutputStream bStream = new BufferedOutputStream(new FileOutputStream(file));
        bitmap.compress(CompressFormat.JPEG, 100, bStream);
        bStream.flush();
        bStream.close();
        bitmap.recycle();
    } catch (IOException e) {
        // Log.d(TAG, e.toString());
    }
}