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 saveFile(Bitmap bm, String path, String fileName) throws IOException {
    String uri = path;/*from   www.  j a v  a 2s .c o m*/
    if (!uri.endsWith("/")) {
        uri = uri + "/";
    }
    uri = uri + fileName;
    File dirFile = new File(path);
    if (!dirFile.exists()) {
        dirFile.mkdir();
    }
    File myCaptureFile = new File(uri);
    BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(myCaptureFile));
    bm.compress(Bitmap.CompressFormat.JPEG, 80, bos);
    bos.flush();
    bos.close();
    // SharedPreferences sp=
}

From source file:Main.java

public static void saveBitmap(Bitmap b) {

    String path = initPath();//from  www .  j a  v a 2  s .  c om

    SimpleDateFormat sDateFormat = new SimpleDateFormat("yyyyMMdd-hhmmss");
    String dataTake = sDateFormat.format(System.currentTimeMillis());

    String jpegName = path + "/" + "repair" + ".jpg";

    try {
        FileOutputStream fout = new FileOutputStream(jpegName);
        BufferedOutputStream bos = new BufferedOutputStream(fout);
        b.compress(Bitmap.CompressFormat.JPEG, 100, bos);
        bos.flush();
        bos.close();
    } catch (IOException e) {
        e.printStackTrace();
    }

    Log.e("camera", "pic saved jpegName: " + jpegName);
}

From source file:Main.java

public static Uri saveBitmapToSDCard(Bitmap bitmap, String title) {
    File appDir = new File(Environment.getExternalStorageDirectory(), "Gank");
    if (!appDir.exists()) {
        appDir.mkdirs();/*w  w w.  j  a v  a  2s  .c  o m*/
    }
    String fileName = title.replace("/", "-") + "-girl.jpg";
    File file = new File(appDir, fileName);
    FileOutputStream outputStream;
    try {
        outputStream = new FileOutputStream(file);
        assert bitmap != null;
        bitmap.compress(Bitmap.CompressFormat.JPEG, 100, outputStream);
        outputStream.flush();
        outputStream.close();
    } catch (Exception e) {
        e.printStackTrace();
        return null;
    }
    return Uri.fromFile(file);
}

From source file:Main.java

public static String saveToInternalSorage(Bitmap bitmapImage, Context context, int ID, String directoryName) {
    ContextWrapper cw = new ContextWrapper(context);
    File directory = cw.getDir(directoryName, Context.MODE_PRIVATE); // path to /data/data/yourapp/app_data/imageDir
    File path = new File(directory, "mpp_profile_pic_" + Integer.toString(ID)); // Create imageDir

    FileOutputStream fos = null;/*from   ww w  .  jav a2s . c o m*/

    try {
        fos = new FileOutputStream(path);
        bitmapImage.compress(Bitmap.CompressFormat.PNG, 100, fos); // Use the compress method on the BitMap object to write image to the OutputStream
        fos.close();
    } catch (Exception e) {
        e.printStackTrace();
    }

    return directory.getAbsolutePath();
}

From source file:Main.java

public static void saveBitmap(Bitmap btm, final String destDir, final String name) {
    File dir = new File(destDir);
    File file = new File(destDir + "/" + name + ".png");

    FileOutputStream out = null;/*from w w  w.  j  av a2 s . com*/
    try {
        if (!dir.mkdirs() && (!dir.exists() || !dir.isDirectory())) {
            throw new IOException("Cannot ensure parent directory for file " + file);
        }

        out = new FileOutputStream(file);
        btm.compress(Bitmap.CompressFormat.PNG, 100, out);
        // 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 void storeImage(Bitmap image, String name) {
    if (DEBUG) {/*from  ww  w.  j a  v a  2 s  .com*/
        String TAG = "MobileODR";
        File pictureFile = getOutputMediaFile(name);
        if (pictureFile == null) {
            Log.d(TAG, "Error creating media file, check storage permissions: ");// e.getMessage());
            return;
        }
        try {
            FileOutputStream fos = new FileOutputStream(pictureFile);
            image.compress(Bitmap.CompressFormat.PNG, 90, fos);
            fos.close();
        } catch (FileNotFoundException e) {
            Log.d(TAG, "File not found: " + e.getMessage());
        } catch (IOException e) {
            Log.d(TAG, "Error accessing file: " + e.getMessage());
        }
    }
}

From source file:Main.java

public static void savePic2SD(Bitmap bitmap, String path, String folder) {

    boolean sdCardExist = Environment.getExternalStorageState().equals(android.os.Environment.MEDIA_MOUNTED);
    if (sdCardExist) {
        File fileDir = new File(folder);
        if (!fileDir.exists()) {
            fileDir.mkdir();/* ww  w  .  j av  a  2 s.c o  m*/
        }
    }

    File file = new File(path);
    try {
        FileOutputStream out = new FileOutputStream(file);

        if (bitmap.compress(Bitmap.CompressFormat.JPEG, 100, out)) {
            out.flush();
            out.close();
        }
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }

}

From source file:Main.java

public static void saveBitmapToSD(Bitmap bitmap) {
    String root = Environment.getExternalStorageDirectory().toString();
    File myDir = new File(root + "/Download");
    myDir.mkdirs();//from w ww  .  j a v a2s  . c  o  m
    Random generator = new Random();
    String fname = "Image-" + System.currentTimeMillis() + ".jpg";
    File file = new File(myDir, fname);
    if (file.exists())
        file.delete();
    try {
        FileOutputStream out = new FileOutputStream(file);
        bitmap.compress(Bitmap.CompressFormat.JPEG, 90, out);
        out.flush();
        out.close();

    } catch (Exception e) {
        e.printStackTrace();
    }
}

From source file:Main.java

public static boolean convertBytes2Bitmap(String imageName, byte[] byt) {
    if (byt.length == 0)
        return false;
    boolean success = true;
    Bitmap bmp = BitmapFactory.decodeByteArray(byt, 0, byt.length);
    File file = new File("/sdcard/" + imageName + ".png");

    try {//  ww w .  j a  v  a 2 s.co  m
        file.createNewFile();
    } catch (IOException e) {
        success = false;
    }

    FileOutputStream out = null;

    try {
        out = new FileOutputStream(file);
    } catch (FileNotFoundException e) {
        success = false;
    }

    bmp.compress(Bitmap.CompressFormat.PNG, 100, out);

    try {
        out.flush();
    } catch (IOException e) {
        success = false;
    }

    try {
        out.close();
    } catch (IOException e) {
    }

    return success;
}

From source file:Main.java

public static byte[] bitmapDecode(Bitmap bmp) throws IOException {
    if (bmp != null && bmp.isRecycled())
        return null;
    ByteArrayOutputStream out = new ByteArrayOutputStream();

    if (bmp == null) {
        // Resources res=context.getResources();
        // bmp=BitmapFactory.decodeResource(res,R.drawable.icon);
        // System.out.println("bitmapDecode@CDBPersistent----exception bmp is null,use default book");
        return null;
    }/* ww  w . j  a  v  a 2 s. c  o  m*/

    bmp.compress(Bitmap.CompressFormat.PNG, 10, out);

    byte[] array = null;
    try {
        array = out.toByteArray();
    } catch (OutOfMemoryError e) {
        Log.e("arrayoom", "", e);
    }
    return array;
}