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

/**
 * Creates a File from a Bitmap/*  w ww .  ja  v a 2s .c o m*/
 *
 * @param bitmap to convert in a file
 *
 * @return File
 */
public static File createFileFromBitmap(Bitmap bitmap) {

    if (bitmap == null)
        return null;

    File photoFile = null;

    ByteArrayOutputStream bytes = new ByteArrayOutputStream();
    bitmap.compress(Bitmap.CompressFormat.PNG, 100, bytes);
    File photoStorage = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES);
    if (photoStorage != null) {
        photoFile = new File(photoStorage, (System.currentTimeMillis()) + ".png");
        try {
            //f.createNewFile();
            FileOutputStream fo = new FileOutputStream(photoFile);
            fo.write(bytes.toByteArray());
            fo.flush();
            fo.close();
        } catch (IOException e) {
            Log.e(TAG, "Error saving image ", e);
        }
    }

    return photoFile;
}

From source file:Main.java

public static void saveBitmap(Bitmap bitmap, Bitmap.CompressFormat format, File target) {
    if (target.exists()) {
        target.delete();/*  ww  w.  j  ava  2  s .  co m*/
    }
    try {
        FileOutputStream out = new FileOutputStream(target);
        bitmap.compress(format, 100, out);
        out.flush();
        out.close();
    } catch (Exception e) {
        e.printStackTrace();
    }
}

From source file:Main.java

/**
 * Tries to save the picked picture to external storage, this makes it possible to use the same function for
 * retrieving the picture//  ww  w.  ja  v  a2s . c o  m
 *
 * @param pBitmap
 *        The Bitmap we want to save to external storage
 * @param pPhotoPath
 *        The path the photo should be saved to
 */
public static void savePictureToExternalStorage(Bitmap pBitmap, String pPhotoPath) {
    if (pBitmap == null) {
        return;
    }

    FileOutputStream out = null;
    try {
        out = new FileOutputStream(pPhotoPath);
        pBitmap.compress(Bitmap.CompressFormat.JPEG, 100, out);
    } catch (Exception e) {
        e.printStackTrace();
    } finally {
        try {
            if (out != null) {
                out.close();
            }
        } catch (Throwable ignore) {
            // We don't have to do anything
        }
    }
}

From source file:Main.java

public static String convertIconToString(Bitmap bitmap) {
    String result = null;//from   ww  w .  j  a v a  2  s  . c  o m
    ByteArrayOutputStream baos = null;
    try {
        if (bitmap != null) {
            baos = new ByteArrayOutputStream();
            bitmap.compress(Bitmap.CompressFormat.JPEG, 100, baos);

            baos.flush();
            baos.close();

            byte[] bitmapBytes = baos.toByteArray();
            result = Base64.encodeToString(bitmapBytes, Base64.DEFAULT);
        }
    } catch (IOException e) {
        e.printStackTrace();
    } finally {
        try {
            if (baos != null) {
                baos.flush();
                baos.close();
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    return result;

}

From source file:Main.java

public static boolean saveBitmap2File(Bitmap bitmap, File file) {
    FileOutputStream fos = null;/*from ww w  .j a  va  2 s .c om*/
    try {
        file.deleteOnExit();
        file.createNewFile();
        fos = new FileOutputStream(file);
        bitmap.compress(Bitmap.CompressFormat.JPEG, 100, fos);
        fos.flush();
    } catch (IOException e) {
        e.printStackTrace();
        return false;
    } finally {
        if (fos != null) {
            try {
                fos.close();
            } catch (IOException e1) {
                e1.printStackTrace();
            }
        }
    }
    return true;
}

From source file:Main.java

public static String BitmapToBase64(Bitmap bitmap) {

    String result = null;/*  w  w w.ja v  a 2  s .co  m*/
    ByteArrayOutputStream baos = null;
    try {
        if (bitmap != null) {
            baos = new ByteArrayOutputStream();
            bitmap.compress(Bitmap.CompressFormat.JPEG, 60, baos);

            baos.flush();
            baos.close();

            byte[] bitmapBytes = baos.toByteArray();
            result = Base64.encodeToString(bitmapBytes, Base64.DEFAULT);
        }
    } catch (IOException e) {
        e.printStackTrace();
    } finally {
        try {
            if (baos != null) {
                baos.flush();
                baos.close();
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    return result;
}

From source file:Main.java

public static String saveImageToGallery(File file, Bitmap bmp) {
    if (bmp == null) {
        Log.e(TAG, "saveImageToGallery: the bitmap is null");
        return "";
    }//w w  w.  j a v  a2s .co  m
    try {
        FileOutputStream fos = new FileOutputStream(file);
        bmp.compress(Bitmap.CompressFormat.JPEG, 100, fos);
        fos.flush();
        fos.close();
    } catch (IOException e) {
        e.printStackTrace();
    }

    Log.e(TAG, "saveImageToGallery: the path of bmp is " + file.getAbsolutePath());
    return file.getAbsolutePath();
}

From source file:Main.java

public static String saveToInternalSorage(Bitmap bitmapImage, String filename, Context context) {
    File mypath = new File(getImagesDirectory(context), filename);
    FileOutputStream fos = null;/*from w  w  w.  j  a  va2s  .co m*/
    try {
        fos = new FileOutputStream(mypath);
        bitmapImage.compress(Bitmap.CompressFormat.JPEG, 100, fos);
        fos.close();
    } catch (Exception e) {
        e.printStackTrace();
    }
    return mypath.getAbsolutePath();
}

From source file:Main.java

/**
 * Writes a simple bitmap to local storage. The image is a solid color with size 400x400
 *//*from   w w  w . j a  v a2  s  . co  m*/
private static void writeSolidColorBitmapToFile(File file, int color) throws IOException {
    if (!file.exists()) {
        Bitmap bitmap = Bitmap.createBitmap(400, 400, Bitmap.Config.ARGB_8888);
        bitmap.eraseColor(color);

        FileOutputStream fos = null;
        try {
            fos = new FileOutputStream(file);
            bitmap.compress(Bitmap.CompressFormat.PNG, 100, fos);
        } finally {
            if (fos != null) {
                fos.close();
            }
        }
    }
}

From source file:Main.java

public static void saveBitmapToFile(Bitmap bitmap, String filePath) throws IOException {
    makeDirsByFilePath(filePath);/*w w w. j  av a 2  s  .  c o m*/
    File bitmapFile = new File(filePath);
    BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(bitmapFile));
    bitmap.compress(Bitmap.CompressFormat.PNG, 80, bos);
    bos.flush();
    bos.close();
}