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 saveImageToSD(Context ctx, String filePath, Bitmap bitmap, int quality) throws IOException {
    if (bitmap != null) {
        File file = new File(filePath.substring(0, filePath.lastIndexOf(File.separator)));
        if (!file.exists()) {
            file.mkdirs();//from   w ww. ja va 2 s .  co m
        }
        BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(filePath));
        bitmap.compress(CompressFormat.JPEG, quality, bos);
        bos.flush();
        bos.close();
        if (ctx != null) {
            scanPhoto(ctx, filePath);
        }
    }
}

From source file:com.androidex.volley.toolbox.HttpHeaderParser.java

/**
 * Extracts a {@link Cache.Entry} from a {@link NetworkResponse}.
 * Cache-control headers are ignored. SoftTtl == 3 mins, ttl == 24 hours.
 * @param bitmap The network response to parse headers from
 * @return a cache entry for the given response, or null if the response is not cacheable.
 *///from  w w  w.  ja va  2s.co m
public static Cache.Entry parseBitmapCacheHeaders(Bitmap bitmap) {
    NetworkResponse response = null;
    if (null != bitmap) {
        ByteArrayOutputStream stream = new ByteArrayOutputStream();
        bitmap.compress(Bitmap.CompressFormat.JPEG, 70, stream);
        byte[] byteArray = stream.toByteArray();
        response = new NetworkResponse(byteArray);
    }
    return parseCacheHeaders(response);
}

From source file:Main.java

public static String saveToInternalStorage(Bitmap bitmap, String title, Context context) throws IOException {
    ContextWrapper cw = new ContextWrapper(context);
    File directory = cw.getDir("bitmaps", Context.MODE_PRIVATE);
    File image = new File(directory, title);
    FileOutputStream fileOutputStream = null;
    try {//from w ww.j a  va2  s.co  m
        fileOutputStream = new FileOutputStream(image);
        bitmap.compress(Bitmap.CompressFormat.PNG, 100, fileOutputStream);
    } catch (IOException e) {
        e.printStackTrace();
    } finally {
        fileOutputStream.close();
    }
    return directory.getAbsolutePath();
}

From source file:com.kabootar.GlassMemeGenerator.ImageOverlay.java

public static boolean saveToSD(final Bitmap overlaid, final String sdPath, final String fileName) {
    boolean isItSaved = false;
    new AsyncTask<Void, Void, Void>() {

        @Override/*from  w w  w  .  j  av  a  2  s .  c o m*/
        protected Void doInBackground(Void... arg0) {

            File image = new File(sdPath, fileName);

            FileOutputStream outStream;
            try {

                outStream = new FileOutputStream(image);
                //resize image
                Bitmap newoverlaid = getResizedBitmap(overlaid, 1000, 1362);
                newoverlaid.compress(Bitmap.CompressFormat.PNG, 100, outStream);

                outStream.flush();
                outStream.close();
            } catch (FileNotFoundException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }
            return null;
        }

        public Bitmap getResizedBitmap(Bitmap bm, int newHeight, int newWidth) {
            int width = bm.getWidth();
            int height = bm.getHeight();
            float scaleWidth = ((float) newWidth) / width;
            float scaleHeight = ((float) newHeight) / height;
            // CREATE A MATRIX FOR THE MANIPULATION
            Matrix matrix = new Matrix();
            // RESIZE THE BIT MAP
            matrix.postScale(scaleWidth, scaleHeight);

            // "RECREATE" THE NEW BITMAP
            Bitmap resizedBitmap = Bitmap.createBitmap(bm, 0, 0, width, height, matrix, false);
            return resizedBitmap;
        }

        @Override
        protected void onPostExecute(Void result) {
            super.onPostExecute(result);
        }
    }.execute();

    return isItSaved;
}

From source file:Main.java

public static boolean saveFile(Bitmap bm, String path) {
    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(Bitmap.CompressFormat.JPEG, 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 boolean saveScreenshot(Activity activity, String fileName, Bitmap screenshot, boolean sdcard) {
    try {/*from w  w w .j  a  v a2s  .  co  m*/
        FileOutputStream fos = null;
        if (!sdcard) {
            fos = activity.openFileOutput(fileName, Context.MODE_WORLD_READABLE);
        } else {
            File f = new File(fileName);
            f.createNewFile();
            fos = new FileOutputStream(f);
        }
        screenshot.compress(Bitmap.CompressFormat.JPEG, 70, fos);
        fos.flush();
        fos.close();
        return true;
    } catch (IOException e) {
        e.printStackTrace();
    }
    return false;
}

From source file:Main.java

public static boolean savePngScreenshot(Activity activity, String fileName, Bitmap screenshot, boolean sdcard) {
    try {/*from www  .  j  a  v  a2s  . c  o m*/
        FileOutputStream fos = null;
        if (!sdcard) {
            fos = activity.openFileOutput(fileName, Context.MODE_WORLD_READABLE);
        } else {
            File f = new File(fileName);
            f.createNewFile();
            fos = new FileOutputStream(f);
        }
        screenshot.compress(Bitmap.CompressFormat.PNG, 70, fos);
        fos.flush();
        fos.close();
        return true;
    } catch (IOException e) {
        e.printStackTrace();
    }
    return false;
}

From source file:Main.java

static boolean saveBitmap2file(Bitmap bmp, String filename) {
    Bitmap.CompressFormat format = Bitmap.CompressFormat.JPEG;
    int quality = 100;
    OutputStream stream = null;//from  w  ww.  j  a  v a 2  s .  co  m
    try {
        stream = new FileOutputStream("/sdcard/" + filename);
    } catch (FileNotFoundException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

    return bmp.compress(format, quality, stream);
}

From source file:Main.java

public static void saveMyBitmap(String bitName, Bitmap mBitmap) {
    File f = new File(bitName);
    try {/*from www .j a va 2  s  . co  m*/
        f.createNewFile();
    } catch (IOException e) {
        e.printStackTrace();
    }
    FileOutputStream fOut = null;
    try {
        fOut = new FileOutputStream(f);
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    }
    mBitmap.compress(Bitmap.CompressFormat.PNG, 90, fOut);
    try {
        fOut.flush();
    } catch (IOException e) {
        e.printStackTrace();
    }
    try {
        fOut.close();
    } catch (IOException e) {
        e.printStackTrace();
    }
}

From source file:Main.java

/**
 * Write the given bitmap to a file in the external storage. Requires 
 * "android.permission.WRITE_EXTERNAL_STORAGE" permission.
 *//*ww w.  j  a v  a2s.  c o m*/
public static void writeToFile(Bitmap bitmap, String dir, String filename)
        throws FileNotFoundException, IOException {
    File sdCard = Environment.getExternalStorageDirectory();
    File dirFile = new File(sdCard.getAbsolutePath() + "/" + dir);
    dirFile.mkdirs();
    File f = new File(dirFile, filename);
    FileOutputStream fos = new FileOutputStream(f, false);
    bitmap.compress(CompressFormat.PNG, 100 /*ignored for PNG*/, fos);
    fos.flush();
    fos.close();
}