2016-09-07 278 views
3

我的服務器限制圖片大小上傳圖片(2MB)。 我想從android設備上傳圖片到服務器。我想調整圖像大小。什麼是我可以調整圖像大小的最佳方式?[Android] - 將圖片上傳到服務器

+0

cropping image? –

+0

@vrundpurohit是的,我有圖像5MB,我想調整到2MB –

+0

您可以使用_GZip_或者您可以發送圖像作爲字節數組! – Piyush

回答

8

使用下面的代碼。使用「MAX_IMAGE_SIZE」,以千字節爲單位指定最大文件大小。在這個代碼中;首先我調整圖像大小,然後壓縮它。在代碼中查看註釋以更好地理解邏輯。

 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; 
} 
+0

爲什麼這比其他答案更好?是因爲這壓縮文件的大小和質量?即使它更復雜。 –

+0

偉大的解決方案。幫助我出去了一堆! – Nikaoto

0
Bitmap scaledBitmap = scaleDown(realImage, MAX_IMAGE_SIZE, true); 

比例縮小方法:

public static Bitmap scaleDown(Bitmap realImage, float maxImageSize, 
    boolean filter) { 
float ratio = Math.min(
     (float) maxImageSize/realImage.getWidth(), 
     (float) maxImageSize/realImage.getHeight()); 
int width = Math.round((float) ratio * realImage.getWidth()); 
int height = Math.round((float) ratio * realImage.getHeight()); 

Bitmap newBitmap = Bitmap.createScaledBitmap(realImage, width, 
     height, filter); 
return newBitmap; 

}