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 String saveBitmapInSdCard(Bitmap bitmap, String fileName) {
    FileOutputStream fos = null;/* w  ww.  j av  a2  s  .  c o  m*/
    File file = null;
    String path;
    try {
        file = new File(createFile(IMAGE_PATH, fileName));
        fos = new FileOutputStream(file);
        if (fos != null) {
            bitmap.compress(Bitmap.CompressFormat.JPEG, 90, fos);
            fos.close();
        }
    } catch (Exception e) {
        e.printStackTrace();
        return null;
    }
    return file.getAbsolutePath();
}

From source file:Main.java

/**
 * @param context/*www.  ja v  a 2 s .c om*/
 * @param data
 * @return
 */
@Nullable
public static byte[] retrieveSelectedImage(@NonNull Context context, @NonNull Intent data) {
    InputStream inStream = null;
    Bitmap bitmap = null;
    try {
        inStream = context.getContentResolver().openInputStream(data.getData());
        bitmap = BitmapFactory.decodeStream(inStream);
        final ByteArrayOutputStream outStream = new ByteArrayOutputStream();
        bitmap.compress(Bitmap.CompressFormat.JPEG, 100, outStream);
        return outStream.toByteArray();
    } catch (FileNotFoundException e) {
        return null;
    } finally {
        if (inStream != null) {
            try {
                inStream.close();
            } catch (IOException ignored) {
            }
        }
        if (bitmap != null) {
            bitmap.recycle();
        }
    }
}

From source file:Main.java

public static boolean scaleImage(String origin_path, String result_path, int target_size) {
    BitmapFactory.Options options = new BitmapFactory.Options();
    options.inJustDecodeBounds = true;/* w  w w. jav a2s . c  o m*/
    BitmapFactory.decodeFile(origin_path, options);
    int imageHeight = options.outHeight;
    int imageWidth = options.outWidth;
    int scale = (int) (Math.sqrt(imageWidth * imageHeight * 4 / target_size)) + 1;
    Bitmap bmp = null;
    options.inJustDecodeBounds = false;
    options.inSampleSize = scale;
    bmp = BitmapFactory.decodeFile(origin_path, options);

    FileOutputStream out = null;
    try {
        out = new FileOutputStream(result_path);
        bmp.compress(Bitmap.CompressFormat.PNG, 100, out);
        return true;
    } catch (Exception e) {
        e.printStackTrace();
        return false;
    } finally {
        try {
            bmp.recycle();
            if (out != null) {
                out.close();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

From source file:Main.java

public static void savePic(File targetFile, Bitmap bitmap) throws IOException {

    if (!targetFile.getParentFile().exists()) {
        targetFile.getParentFile().mkdirs();
    } else if (!targetFile.exists()) {
        targetFile.createNewFile();//from   w  ww . j av  a  2 s .  c  om
    } else {
        FileOutputStream fos = new FileOutputStream(targetFile);
        bitmap.compress(Bitmap.CompressFormat.JPEG, 100, fos);
    }
}

From source file:Main.java

public static File saveBitmap(Bitmap bitmap, String path, String picName) {
    File dir = new File(path);
    if (dir != null && !dir.exists()) {
        dir.mkdirs();/*from   w  ww  .  j ava  2s  .  com*/
    }
    File file = new File(dir, picName);
    try {
        FileOutputStream fos = new FileOutputStream(file);
        bitmap.compress(CompressFormat.PNG, 100, fos);
        fos.close();
    } catch (IOException e) {
        e.printStackTrace();
        return null;
    }
    return file;
}

From source file:Main.java

/**
 * The following method creates a File from a given Bitmap.
 * //from   w  w  w.  j ava2  s  .  c om
 * @param context
 *            - The application context.
 * @param bitmap
 *            - The bitmap to be converted into a File.
 * @return
 */
public static File getFilefromBitmap(Context context, Bitmap bitmap) {
    File file = null;
    try {
        String path = Environment.getExternalStorageDirectory().toString();
        OutputStream fOut = null;
        file = new File(path, "1" + ".jpg");
        fOut = new FileOutputStream(file);

        bitmap.compress(Bitmap.CompressFormat.PNG, 100, fOut);
        fOut.flush();
        fOut.close();
        MediaStore.Images.Media.insertImage(context.getContentResolver(), file.getAbsolutePath(),
                file.getName(), file.getName());
    } catch (Exception e) {
        e.printStackTrace();
    }
    return file;

}

From source file:Main.java

public static String getThumbnailImage(String path, int targetWidth) {
    Bitmap scaleImage = decodeScaleImage(path, targetWidth, targetWidth);
    try {/*from w w  w  .ja  v  a 2  s .com*/
        File file = File.createTempFile("image", ".jpg");
        FileOutputStream fileOutputStream = new FileOutputStream(file);
        scaleImage.compress(Bitmap.CompressFormat.JPEG, 60, fileOutputStream);
        fileOutputStream.close();
        return file.getAbsolutePath();
    } catch (Exception e) {
        e.printStackTrace();
        return path;
    }
}

From source file:Main.java

public static String writeBitmap(byte[] data, int cameraDegree, Rect rect, Rect willTransformRect)
        throws IOException {
    File file = new File(Environment.getExternalStorageDirectory() + "/bookclip/");
    file.mkdir();//from   w w w  .jav  a  2 s  . c om
    String bitmapPath = file.getPath() + "/" + System.currentTimeMillis() + ".png";

    // bitmap rotation, scaling, crop
    BitmapFactory.Options option = new BitmapFactory.Options();
    option.inSampleSize = 2;
    Bitmap bitmap = BitmapFactory.decodeByteArray(data, 0, data.length, option);
    Matrix bitmapMatrix = new Matrix();
    bitmapMatrix.setRotate(cameraDegree);
    int x = rect.left, y = rect.top, width = rect.right - rect.left, height = rect.bottom - rect.top;
    Bitmap rotateBitmap = Bitmap.createBitmap(bitmap, 0, 0, bitmap.getWidth(), bitmap.getHeight(), bitmapMatrix,
            false);
    // bitmap recycle
    bitmap.recycle();

    Bitmap scaledBitmap = Bitmap.createScaledBitmap(rotateBitmap, willTransformRect.right,
            willTransformRect.bottom - willTransformRect.top, false);
    // rotatebitmap recycle
    rotateBitmap.recycle();

    Bitmap cropBitmap = Bitmap.createBitmap(scaledBitmap, x, y, width, height, null, false);
    // scaledBitmap recycle
    scaledBitmap.recycle();

    // file write
    FileOutputStream fos = new FileOutputStream(new File(bitmapPath));
    cropBitmap.compress(CompressFormat.PNG, 100, fos);
    fos.flush();
    fos.close();

    // recycle
    cropBitmap.recycle();

    return bitmapPath;
}

From source file:com.radadev.xkcd.Comics.java

public static void downloadComic(Integer comicNumber, Context context)
        throws FileNotFoundException, IOException {
    ComicDbAdapter dbAdapter = new ComicDbAdapter(context);
    dbAdapter.open();//from w  w w .  ja v  a 2  s  . c  o m
    try {
        dbAdapter.updateComic(comicNumber);
        File file = new File(getSdDir(context), comicNumber.toString() + ".png");
        if (file.length() <= 0) {
            Cursor cursor = dbAdapter.fetchComic(comicNumber);
            String url = cursor.getString(cursor.getColumnIndexOrThrow(Comics.SQL_KEY_IMAGE));

            if (url == null || url.length() == 0) {
                dbAdapter.updateComic(comicNumber);
                cursor.close();
                cursor = dbAdapter.fetchComic(comicNumber);
                url = cursor.getString(cursor.getColumnIndexOrThrow(Comics.SQL_KEY_IMAGE));
            }
            cursor.close();

            Bitmap bitmap = Comics.downloadBitmap(url);
            FileOutputStream fileStream = new FileOutputStream(file);
            bitmap.compress(CompressFormat.PNG, 100, fileStream);
            bitmap.recycle();
            fileStream.close();
        }
    } finally {
        dbAdapter.close();
    }
}

From source file:Main.java

public static String SaveBitmap(Bitmap bmp, String name) {
    File file = new File("mnt/sdcard/picture/");
    String path = null;/*from ww  w  .  j a v a2  s  . c  o m*/
    if (!file.exists())
        file.mkdirs();
    try {
        path = file.getPath() + "/" + name;
        FileOutputStream fileOutputStream = new FileOutputStream(path);

        bmp.compress(Bitmap.CompressFormat.JPEG, 100, fileOutputStream);
        fileOutputStream.flush();
        fileOutputStream.close();
        System.out.println("saveBmp is here");
    } catch (Exception e) {
        e.printStackTrace();
    }

    return path;
}