Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

[Android]-Resize image to upload to server

Tags:

android

My sever limit image size to upload image (2MB). I want to upload image from android device to server. I want to resize image. What's the best way I can do resize image?

like image 647
Danh Nguyen Avatar asked Sep 07 '16 05:09

Danh Nguyen


1 Answers

Use the code below. With "MAX_IMAGE_SIZE" specify your max file size in kilobayts. In this code; first i resize the image, then i compress it. See comments in the code to understand the logic better.

     public static String resizeAndCompressImageBeforeSend(Context context,String filePath,String fileName){
     final int MAX_IMAGE_SIZE = 700 * 1024; // max final file size in kilobytes

     // First decode with inJustDecodeBounds=true to check dimensions of image
     final BitmapFactory.Options options = new BitmapFactory.Options();
     options.inJustDecodeBounds = true;
     BitmapFactory.decodeFile(filePath,options);

     // Calculate inSampleSize(First we are going to resize the image to 800x800 image, in order to not have a big but very low quality image.
     //resizing the image will already reduce the file size, but after resizing we will check the file size and start to compress image
     options.inSampleSize = calculateInSampleSize(options, 800, 800);

     // Decode bitmap with inSampleSize set
     options.inJustDecodeBounds = false;
     options.inPreferredConfig= Bitmap.Config.ARGB_8888;

     Bitmap bmpPic = BitmapFactory.decodeFile(filePath,options);


     int compressQuality = 100; // quality decreasing by 5 every loop.
     int streamLength;
     do{
         ByteArrayOutputStream bmpStream = new ByteArrayOutputStream();
         Log.d("compressBitmap", "Quality: " + compressQuality);
         bmpPic.compress(Bitmap.CompressFormat.JPEG, compressQuality, bmpStream);
         byte[] bmpPicByteArray = bmpStream.toByteArray();
         streamLength = bmpPicByteArray.length;
         compressQuality -= 5;
         Log.d("compressBitmap", "Size: " + streamLength/1024+" kb");
     }while (streamLength >= MAX_IMAGE_SIZE);

     try {
         //save the resized and compressed file to disk cache
         Log.d("compressBitmap","cacheDir: "+context.getCacheDir());
         FileOutputStream bmpFile = new FileOutputStream(context.getCacheDir()+fileName);
         bmpPic.compress(Bitmap.CompressFormat.JPEG, compressQuality, bmpFile);
         bmpFile.flush();
         bmpFile.close();
     } catch (Exception e) {
         Log.e("compressBitmap", "Error on saving file");
     }
     //return the path of resized and compressed file
     return  context.getCacheDir()+fileName;
 }



 public static int calculateInSampleSize(BitmapFactory.Options options, int reqWidth, int reqHeight) {
     String debugTag = "MemoryInformation";
     // Image nin islenmeden onceki genislik ve yuksekligi
     final int height = options.outHeight;
     final int width = options.outWidth;
     Log.d(debugTag,"image height: "+height+ "---image width: "+ width);
     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 *= 2;
         }
     }
     Log.d(debugTag,"inSampleSize: "+inSampleSize);
     return inSampleSize;
 }
like image 145
Eren Avatar answered Sep 27 '22 17:09

Eren