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 createScaledImage(String sourceFile, String destinationFile, int desiredWidth,
        int desiredHeight) {
    BitmapFactory.Options options = new BitmapFactory.Options();
    options.inJustDecodeBounds = true;//  w w  w. j ava2s  .co m
    BitmapFactory.decodeFile(sourceFile, options);

    int srcWidth = options.outWidth;
    int srcHeight = options.outHeight;

    if (desiredWidth > srcWidth) {
        desiredWidth = srcWidth;
    }

    int inSampleSize = 1;
    while (srcWidth / 2 > desiredWidth) {
        srcWidth /= 2;
        srcHeight /= 2;
        inSampleSize *= 2;
    }

    float desiredScale = (float) desiredWidth / srcWidth;

    options.inJustDecodeBounds = false;
    options.inDither = false;
    options.inSampleSize = inSampleSize;
    options.inScaled = false;
    options.inPreferredConfig = Bitmap.Config.ARGB_8888;
    Bitmap sampledSrcBitmap = BitmapFactory.decodeFile(sourceFile, options);

    Matrix matrix = new Matrix();
    matrix.postScale(desiredScale, desiredScale);
    Bitmap scaledBitmap = Bitmap.createBitmap(sampledSrcBitmap, 0, 0, sampledSrcBitmap.getWidth(),
            sampledSrcBitmap.getHeight(), matrix, true);
    sampledSrcBitmap = null;

    try {
        FileOutputStream out = new FileOutputStream(destinationFile);
        scaledBitmap.compress(Bitmap.CompressFormat.JPEG, 85, out);
        scaledBitmap = null;
    } catch (IOException e) {
        e.printStackTrace();
    }
}

From source file:Main.java

public static String scaleImageFile(String filepath, int targetSize) {
    BitmapFactory.Options options = new BitmapFactory.Options();
    File file = new File(filepath);
    float scaleFactor = file.length() / targetSize;
    options.inSampleSize = Math.round(scaleFactor);
    Bitmap bitmap = BitmapFactory.decodeFile(filepath, options);

    int dotPos = filepath.lastIndexOf('.');
    String newFilePath = String.format("%s_scaled.%s", filepath.substring(0, dotPos),
            filepath.substring(dotPos + 1, filepath.length()));
    FileOutputStream fos = null;/*from w ww  .j  a  v a2s  .co m*/
    try {
        fos = new FileOutputStream(newFilePath);
        if (!bitmap.compress(CompressFormat.JPEG, 90, fos)) {
            newFilePath = null;
        }
    } catch (FileNotFoundException e) {
        e.printStackTrace();
        newFilePath = null;
    }
    bitmap.recycle();
    return newFilePath;
}

From source file:Main.java

/**
 * Take the screen shot of the device//from www .java2 s  . c  o  m
 * 
 * @param view
 */
public static void screenShotMethod(View view) {
    Bitmap bitmap;
    if (view != null) {
        View v1 = view;
        v1.setDrawingCacheEnabled(true);
        v1.buildDrawingCache(true);
        bitmap = Bitmap.createBitmap(v1.getDrawingCache());
        v1.setDrawingCacheEnabled(false);

        ByteArrayOutputStream bytes = new ByteArrayOutputStream();
        bitmap.compress(Bitmap.CompressFormat.JPEG, 100, bytes);
        File f = new File(Environment.getExternalStorageDirectory().getAbsolutePath() + File.separator
                + "CySmart" + File.separator + "file.jpg");
        try {
            FileOutputStream fo = new FileOutputStream(f);
            fo.write(bytes.toByteArray());
            fo.flush();
            fo.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

}

From source file:Main.java

public static void saveMyBitmap(Bitmap mBitmap) {
    File f = new File("/sdcard/head.png");
    try {// www.  j ava2s  .  c  o  m
        f.createNewFile();
    } catch (IOException e) {
        // TODO Auto-generated catch block
    }
    FileOutputStream fOut = null;
    try {
        fOut = new FileOutputStream(f);
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    }
    mBitmap.compress(Bitmap.CompressFormat.PNG, 100, fOut);
    try {
        fOut.flush();
    } catch (IOException e) {
        e.printStackTrace();
    }
    try {
        fOut.close();
    } catch (IOException e) {
        e.printStackTrace();
    }
}

From source file:Main.java

public static void takeScreenshot(String where, String name, View v) {

    // image naming and path to include sd card appending name you choose
    // for file/* www  .java  2  s.c  o  m*/
    String mPath = where + "/" + name;

    // create bitmap screen capture
    Bitmap bitmap;
    View v1 = v.getRootView();
    v1.setDrawingCacheEnabled(true);
    bitmap = Bitmap.createBitmap(v1.getDrawingCache());
    v1.setDrawingCacheEnabled(false);

    OutputStream fout = null;
    File imageFile = new File(mPath);

    try {
        fout = new FileOutputStream(imageFile);
        bitmap.compress(Bitmap.CompressFormat.PNG, 90, fout);
        fout.flush();
        fout.close();

    } catch (FileNotFoundException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

}

From source file:Main.java

public static boolean saveFile(Bitmap bm, String path, Bitmap.CompressFormat format) {
    if (bm == null || path == null)
        return false;
    File myCaptureFile = new File(path);
    if (myCaptureFile.exists()) {
        myCaptureFile.delete();//  w  w w . j av  a  2 s.c  o m
    }
    try {
        BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(myCaptureFile));
        bm.compress(format, 80, bos);
        bos.flush();
        bos.close();
        return true;
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }
    return false;
}

From source file:Main.java

public static void savePhotoToSDCard(Bitmap photoBitmap, String path, String photoName) {
    File dir = new File(path);
    if (!dir.exists()) {
        dir.mkdirs();//ww w .j  a va  2 s . co m
    }
    File photoFile = new File(path, photoName);
    FileOutputStream fileOutputStream = null;
    try {
        fileOutputStream = new FileOutputStream(photoFile);
        if (photoBitmap != null) {
            if (photoBitmap.compress(Bitmap.CompressFormat.PNG, 100, fileOutputStream)) {
                fileOutputStream.flush();
            }
        }
    } catch (FileNotFoundException e) {
        photoFile.delete();
        e.printStackTrace();
    } catch (IOException e) {
        photoFile.delete();
        e.printStackTrace();
    } finally {
        try {
            fileOutputStream.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

From source file:com.easy.facebook.android.util.Util.java

public static byte[] loadPicture(String urlPicture) {
    byte[] pictureData = null;

    try {/*from  www. j a v  a2s.com*/
        URL uploadFileUrl = null;
        try {
            uploadFileUrl = new URL(urlPicture);
        } catch (Exception e) {
            e.printStackTrace();
        }

        HttpURLConnection conn = (HttpURLConnection) uploadFileUrl.openConnection();
        conn.setDoInput(true);
        conn.connect();
        InputStream fis = null;
        try {
            fis = conn.getInputStream();
        } catch (IOException e) {

            e.printStackTrace();
        }
        Bitmap bi = BitmapFactory.decodeStream(fis);
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        bi.compress(Bitmap.CompressFormat.JPEG, 100, baos);
        pictureData = baos.toByteArray();
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    } catch (IOException e) {

        e.printStackTrace();
    }

    return pictureData;
}

From source file:Main.java

public static void base64ToBitmap(String base64Data, String imgName, String imgFormat) {
    byte[] bytes = Base64.decode(base64Data, Base64.DEFAULT);
    Bitmap bitmap = BitmapFactory.decodeByteArray(bytes, 0, bytes.length);

    File myCaptureFile = new File("/sdcard/", imgName);
    FileOutputStream fos = null;//from   w w  w . j  a  va 2  s.c om
    try {
        fos = new FileOutputStream(myCaptureFile);
    } catch (FileNotFoundException e) {
        // TODO Auto-generated catch block  
        e.printStackTrace();
    }
    boolean isTu = bitmap.compress(Bitmap.CompressFormat.JPEG, 100, fos);
    if (isTu) {
        // fos.notifyAll();  
        try {
            fos.flush();
            fos.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    } else {
        try {
            fos.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

From source file:Main.java

public static File saveBmpFile(Bitmap bmp, String filePath, String fileName) {
    File dirFile = new File(filePath);
    if (!dirFile.exists()) {
        dirFile.mkdir();/*  w  w w  . j av a2  s  .c  o  m*/
    }
    File wantSaveFile = new File(filePath + "/" + fileName);
    try {
        BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(wantSaveFile));
        if (fileName.contains(".jpg")) {
            bmp.compress(Bitmap.CompressFormat.JPEG, 100, bos);
        } else {
            bmp.compress(Bitmap.CompressFormat.PNG, 100, bos);
        }

        bos.flush();
        bos.close();
    } catch (IOException e) {
        e.printStackTrace();
    }
    return wantSaveFile;
}