4

After capturing an image and displaying it in activity, it won't insert into the database due to large image size. I need to know how to compress and decrease the image size and pixels.

         try {
              //  Bitmap bitmap;
                BitmapFactory.Options bitmapOptions = new BitmapFactory.Options();

                bitmap = BitmapFactory.decodeFile(f.getAbsolutePath(),
                        bitmapOptions);

                imageView.setImageBitmap(bitmap);

                String path = android.os.Environment
                        .getExternalStorageDirectory()
                        + File.separator
                        + "Phoenix" + File.separator + "default";
                f.delete();
                OutputStream outFile = null;
                File file = new File(path, String.valueOf(System.currentTimeMillis()) + ".jpg");
                try {
                    outFile = new FileOutputStream(file);
                    bitmap.compress(Bitmap.CompressFormat.JPEG, 85, outFile);
                    outFile.flush();
                    outFile.close();
                } catch (FileNotFoundException e) {
                    e.printStackTrace();
Ramesh K R
  • 173
  • 1
  • 5

3 Answers3

2
   try {
          BitmapFactory.Options bitmapOptions = new BitmapFactory.Options();

          bitmap = BitmapFactory.decodeFile(f.getAbsolutePath(),
                        bitmapOptions);

          bitmap =getResizedBitmap(bitmap); 
          imageView.setImageBitmap(bitmap);


                String path = android.os.Environment
                        .getExternalStorageDirectory()
                        + File.separator
                        + "Phoenix" + File.separator + "default";
                f.delete();
                OutputStream outFile = null;
                File file = new File(path, String.valueOf(System.currentTimeMillis()) + ".jpg");
                try {
                    outFile = new FileOutputStream(file);
                    bitmap.compress(Bitmap.CompressFormat.JPEG, 85, outFile);
                    outFile.flush();
                    outFile.close();
                } catch (FileNotFoundException e) {
                    e.printStackTrace();
            }

 public Bitmap getResizedBitmap(Bitmap myBitmap) {
        final int maxSize = 1024;
        int outWidth;
        int outHeight;
        int inWidth = myBitmap.getWidth();
        int inHeight = myBitmap.getHeight();
        if (inWidth > inHeight) {
            outWidth = maxSize;
            outHeight = (inHeight * maxSize) / inWidth;
        } else {
            outHeight = maxSize;
            outWidth = (inWidth * maxSize) / inHeight;
        }

        return Bitmap.createScaledBitmap(myBitmap, outWidth, outHeight, false);
    }
Sohail Zahid
  • 8,099
  • 2
  • 25
  • 41
2

Answer is in your Question itself,

In your code bitmap compress range is 85

   bitmap.compress(Bitmap.CompressFormat.JPEG, 85, outFile); 

You need to change it to 25

   bitmap.compress(Bitmap.CompressFormat.JPEG, 25, outFile);
Harish Reddy
  • 922
  • 10
  • 20
-2
public static Bitmap decodeSampledBitmapFromResource(String imagePath,
        int reqWidth, int reqHeight) {
    // First decode with inJustDecodeBounds=true to check dimensions
    final BitmapFactory.Options options = new BitmapFactory.Options();
    options.inJustDecodeBounds = true;

    Uri uri = Uri.parse(imagePath);

    BitmapFactory.decodeFile(uri.getPath(), options);

    // Calculate inSampleSize
    options.inSampleSize = calculateInSampleSize(options, reqWidth,
            reqHeight);

    // Decode bitmap with inSampleSize set
    options.inJustDecodeBounds = false;

    options.inPreferredConfig = Bitmap.Config.ARGB_8888;
    options.inDither = true;
    return BitmapFactory.decodeFile(uri.getPath(), options);
}

private static int calculateInSampleSize(BitmapFactory.Options options,
        int reqWidth, int reqHeight) {
    // Raw height and width of image
    final int height = options.outHeight;
    final int width = options.outWidth;
    int inSampleSize = 1;

    if (height > reqHeight || width > reqWidth) {
        final int halfHeight = height / 2;
        final int halfWidth = width / 2;
        // Calculate the largest inSampleSize value that is a power of 2 and
        // keeps both
        // height and width larger than the requested height and width.
        while ((halfHeight / inSampleSize) > reqHeight
                && (halfWidth / inSampleSize) > reqWidth) {
            inSampleSize *= 3;
        }
    }
    return inSampleSize;
}

Try this

darwin
  • 1,524
  • 1
  • 22
  • 32