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 save2Cache(Context context, String cover, Bitmap bitmap) {
    File file = new File(context.getCacheDir(), cover + ".png");
    FileOutputStream fileOutputStream = null;
    try {//from  ww  w  .j a  v a 2 s  .com
        fileOutputStream = new FileOutputStream(file);
        if (bitmap != null) {
            if (bitmap.compress(Bitmap.CompressFormat.PNG, 100, fileOutputStream)) {
                fileOutputStream.flush();
            }
        }
    } catch (FileNotFoundException e) {
        file.delete();
        e.printStackTrace();
    } catch (IOException e) {
        file.delete();
        e.printStackTrace();
    } finally {
        try {
            fileOutputStream.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

From source file:Main.java

/**
 * Write the given bitmap to the given uri using the given compression.
 *//*w w  w .ja  v a  2  s.  c om*/
static void writeBitmapToUri(Context context, Bitmap bitmap, Uri uri, Bitmap.CompressFormat compressFormat,
        int compressQuality) throws FileNotFoundException {
    OutputStream outputStream = null;
    try {
        outputStream = context.getContentResolver().openOutputStream(uri);
        bitmap.compress(compressFormat, compressQuality, outputStream);
    } finally {
        closeSafe(outputStream);
    }
}

From source file:Main.java

private static InputStream bitmap2InputStream(Context context, Bitmap bitmap) throws IOException {
    File file = new File(context.getFilesDir(), "wallpaper.jpg");
    if (file.exists()) {
        file.delete();//from   ww w  .ja  v  a 2  s.  co m
    }
    file.createNewFile();
    FileOutputStream fileOutputStream = new FileOutputStream(file);
    bitmap.compress(Bitmap.CompressFormat.JPEG, 90, fileOutputStream);
    fileOutputStream.close();
    return filePath2InputStream(file.getAbsolutePath());
}

From source file:Main.java

public static File getFile(Context context, Bitmap sourceImg) {
    try {// w  w  w.  j  ava  2 s.c  om
        File f = new File(context.getCacheDir(), System.currentTimeMillis() + "temp.jpg");
        f.createNewFile();
        ByteArrayOutputStream bos = new ByteArrayOutputStream();
        int options = 100;
        sourceImg.compress(Bitmap.CompressFormat.JPEG, options, bos);
        while (bos.toByteArray().length / 1024 > 100) {
            bos.reset();
            options -= 10;
            sourceImg.compress(Bitmap.CompressFormat.JPEG, options, bos);
        }
        byte[] bitmapdata = bos.toByteArray();
        FileOutputStream fos = new FileOutputStream(f);
        fos.write(bitmapdata);
        fos.flush();
        fos.close();
        return f;
    } catch (Exception e) {
        e.printStackTrace();
    }
    return null;
}

From source file:Main.java

public static Bitmap getCompressBitmap(Bitmap bitmap, File file, int quality, int fileSize) {
    Bitmap bmp = null;/* ww  w. ja v a2  s  .  com*/
    ByteArrayOutputStream bos = new ByteArrayOutputStream();
    boolean result = bitmap.compress(Bitmap.CompressFormat.JPEG, quality, bos);
    LOG("getCompressBitmap result: " + result);
    try {
        if (file != null) {
            if (result) {
                byte[] bt = bos.toByteArray();

                FileOutputStream fos = new FileOutputStream(file);
                fos.write(bt);
                fos.close();

                LOG("file.length(): " + file.length());

                if (file.length() > fileSize) {
                    bmp = getCompressBitmap(bmp, file, (int) (quality * 0.8), fileSize);
                } else {
                    bmp = BitmapFactory.decodeFile(file.getPath());
                }
            }

        } else {
            bmp = BitmapFactory.decodeByteArray(bos.toByteArray(), 0, bos.size());
        }
        bos.close();
    } catch (Exception e) {
        LOG("getCompressBitmap result: e" + e.toString());
        e.printStackTrace();
        return null;
    }
    return bmp;
}

From source file:Main.java

public static void saveBitmap(Bitmap bitmap, String descPath) {
    File file = new File(descPath);
    if (!file.getParentFile().exists()) {
        file.getParentFile().mkdirs();/* w  ww  .  ja  v a  2 s  . c o  m*/
    }
    if (!file.exists()) {
        try {
            bitmap.compress(CompressFormat.JPEG, 30, new FileOutputStream(file));
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }
    }

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

From source file:Main.java

public static void saveImage(Context context, String fileName, Bitmap bitmap, int quality) throws IOException {
    if (bitmap == null || fileName == null || context == null)
        return;/*from   ww w . j a v a2s .co m*/

    FileOutputStream fos = context.openFileOutput(fileName, Context.MODE_PRIVATE);
    ByteArrayOutputStream stream = new ByteArrayOutputStream();
    bitmap.compress(Bitmap.CompressFormat.JPEG, quality, stream);
    byte[] bytes = stream.toByteArray();
    fos.write(bytes);
    fos.close();
}

From source file:com.hg.development.apps.messagenotifier_v1.Utils.Utility.java

/**
 * Permet de convertir une photo d'un contact  un objet String.
 * @param bitmap photo du contact  convertir.
 * @return//w  ww  .  j a  va  2s.c  o  m
 */
public static String BitMapToString(Bitmap bitmap) {
    try {
        ByteArrayOutputStream ByteStream = new ByteArrayOutputStream();
        bitmap.compress(Bitmap.CompressFormat.PNG, 100, ByteStream);
        byte[] b = ByteStream.toByteArray();
        String temp = Base64.encodeToString(b, Base64.DEFAULT);

        return temp;
    } catch (Exception ex) {
        return null;
    }
}

From source file:Main.java

public static void saveImage(Context context, String fileName, Bitmap bitmap, int quality) throws IOException {
    if (bitmap == null || fileName == null || context == null)
        return;//from w w  w . ja  v a 2 s  .com

    FileOutputStream fos = context.openFileOutput(fileName, Context.MODE_PRIVATE);
    ByteArrayOutputStream stream = new ByteArrayOutputStream();
    bitmap.compress(CompressFormat.JPEG, quality, stream);
    byte[] bytes = stream.toByteArray();
    fos.write(bytes);
    fos.close();
}

From source file:com.example.pyrkesa.com.example.pyrkesa.home.Room.java

private static Asset createAssetFromBitmap(Bitmap bitmap) {
    final ByteArrayOutputStream byteStream = new ByteArrayOutputStream();
    bitmap.compress(Bitmap.CompressFormat.PNG, 100, byteStream);
    return Asset.createFromBytes(byteStream.toByteArray());
}