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 byte[] bitmapToByteArray(Bitmap srcBitmap, String type) throws IOException {
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    if (type.equals("image/png")) {
        srcBitmap.compress(Bitmap.CompressFormat.PNG, 100, baos);
    } else if (type.equals("image/jpg") || type.equals("image/jpeg")) {
        srcBitmap.compress(Bitmap.CompressFormat.JPEG, 100, baos);
    }/*from   w w w  .  j a  va  2  s . c om*/
    byte[] bMapArray = baos.toByteArray();
    baos.close();
    return bMapArray;
}

From source file:Main.java

public static String getCrop(Activity activity, String name) {
    Bitmap bmp = getCrop(activity);
    File file = new File(activity.getFilesDir(), name);
    FileOutputStream fileOutput = null;
    try {//  w  ww  .j  a  va 2s . co m
        fileOutput = new FileOutputStream(file);
    } catch (FileNotFoundException e) {
        Log.e("golfpad", e.getMessage());
    }
    bmp.compress(CompressFormat.JPEG, 50, fileOutput);
    return file.getAbsolutePath();
}

From source file:Main.java

public static void bitmap2file(Bitmap bitmap, String path) {
    try {/*from  ww w  . j a v  a 2  s . co  m*/
        // create a file to write bitmap data
        File f = new File(path);
        if (f.exists()) {
            f.delete();
            f.createNewFile();
        }
        ByteArrayOutputStream bos = new ByteArrayOutputStream();
        bitmap.compress(CompressFormat.JPEG, 30 /* ignored for PNG */, bos);
        byte[] bitmapdata = bos.toByteArray();
        // write the bytes in file
        FileOutputStream fos = new FileOutputStream(f);
        fos.write(bitmapdata);
        fos.flush();
        fos.close();
    } catch (IOException e) {
        e.printStackTrace();
    }
}

From source file:Main.java

public static void saveBitmap(String path, Bitmap mBitmap) {
    File f = new File(path);
    if (!f.exists())
        createDipPath(path);/*  w ww.  ja  v  a2 s .  c  om*/
    FileOutputStream fOut = null;
    try {
        fOut = new FileOutputStream(f);
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    }
    mBitmap.compress(Bitmap.CompressFormat.JPEG, 100, fOut);
    try {
        fOut.flush();
    } catch (IOException e) {
        e.printStackTrace();
    }
    try {
        fOut.close();
    } catch (IOException e) {
        e.printStackTrace();
    }
}

From source file:Main.java

@SuppressWarnings("ResultOfMethodCallIgnored")
public static boolean saveBitmap(Bitmap bitmap, String filePath, int quality) {
    boolean result = false;
    FileOutputStream fileOutputStream = null;
    try {//w w w. jav a 2  s  .co m
        File file = new File(filePath);
        if (file.exists())
            file.delete();
        fileOutputStream = new FileOutputStream(file);
        bitmap.compress(Bitmap.CompressFormat.JPEG, quality, fileOutputStream);
        fileOutputStream.flush();
        result = true;
    } catch (IOException ignored) {
    } finally {
        if (fileOutputStream != null) {
            try {
                fileOutputStream.close();
            } catch (IOException ignored) {
            }
        }
    }
    return result;
}

From source file:Main.java

public static boolean saveBitmap2File(Bitmap bitmap, String filePath, String fileName) {
    File file = new File(filePath, fileName);
    FileOutputStream fileOutputStream = null;
    try {//from ww  w  . j ava  2  s  .c o m
        if (!file.exists()) {
            file.createNewFile();
        }
        fileOutputStream = new FileOutputStream(file);
        bitmap.compress(Bitmap.CompressFormat.PNG, 100, fileOutputStream);
    } catch (IOException e) {
        e.printStackTrace();
        return false;
    } finally {
        if (fileOutputStream != null) {
            try {
                fileOutputStream.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
    return true;
}

From source file:Main.java

public static boolean StoreByteImage(Context mContext, byte[] imageData, int quality, String expName) {

    File sdImageMainDirectory = new File("/sdcard/myImages");
    FileOutputStream fileOutputStream = null;
    String nameFile = "";
    try {//w w w .  j  a v  a  2s .  c om

        BitmapFactory.Options options = new BitmapFactory.Options();
        options.inSampleSize = 5;

        Bitmap myImage = BitmapFactory.decodeByteArray(imageData, 0, imageData.length, options);

        fileOutputStream = new FileOutputStream(sdImageMainDirectory.toString() + "/" + nameFile + ".jpg");

        BufferedOutputStream bos = new BufferedOutputStream(fileOutputStream);

        myImage.compress(CompressFormat.JPEG, quality, bos);

        bos.flush();
        bos.close();

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

    return true;
}

From source file:Main.java

public static void saveBitmap(Bitmap bm, String picName) {
    try {//from   w  ww  .  j a v  a  2  s .c om
        if (!isFileExist("")) {
            createSDDir("");
        }
        File f = new File(SDPATH, picName);
        if (f.exists()) {
            f.delete();
        }
        FileOutputStream out = new FileOutputStream(f);
        bm.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 boolean saveBitmap(Bitmap bmp, String path) {
    File file = new File(path);
    if (!file.getParentFile().exists()) {
        file.getParentFile().mkdirs();//from  w  w  w. ja  v  a  2s. co  m
    }
    if (file.exists()) {
        file.delete();
    }
    try {

        FileOutputStream out = new FileOutputStream(file);
        bmp.compress(Bitmap.CompressFormat.JPEG, 80, out);
        out.flush();
        out.close();
        Log.i("saveBitmap success:", path);
        return true;
    } catch (Exception e) {
        Log.e("saveBitmap:" + path, e.toString());
    }
    return false;

}

From source file:Main.java

public static void takeScreenshot(String fileName, String directory, View view, int quality) {
    Bitmap bitmap = null;
    FileOutputStream fos = null;//from ww w.j  a v a 2  s .  com
    view.buildDrawingCache(false);
    bitmap = view.getDrawingCache();
    try {
        fos = new FileOutputStream(directory + File.separator + fileName);
        if (fileName.endsWith(".png")) {
            bitmap.compress(Bitmap.CompressFormat.PNG, quality, fos);
        } else if (fileName.endsWith(".jpg")) {
            bitmap.compress(Bitmap.CompressFormat.JPEG, quality, fos);
        }
        fos.flush();
        fos.close();
    } catch (Exception e) {
        Log.e(TAG,
                "Can't save the screenshot! Requires write permission (android.permission.WRITE_EXTERNAL_STORAGE) in AndroidManifest.xml of the application under test.");
        e.printStackTrace();
    }
}