2015-05-05 40 views

回答

1

發送任何文件時,您必須減小大小。

這裏decodeFile()函數將減小圖像的大小,當我們將其轉換爲位圖來顯示選定的圖像。如果我們發送文件,那麼大尺寸的圖像可能會導致應用程序崩潰。您需要2 MB大小的,是的,你可以:

public void decodeFile(String filePath) { 
     // Decode image size 
     BitmapFactory.Options o = new BitmapFactory.Options(); 
     o.inJustDecodeBounds = true; 
     BitmapFactory.decodeFile(filePath, o); 

     // The new size we want to scale to 
     final int REQUIRED_SIZE = 2048; 

     // Find the correct scale value. It should be the power of 2. 
     int width_tmp = o.outWidth, height_tmp = o.outHeight; 
     int scale = 1; 
     while (true) { 
      if (width_tmp < REQUIRED_SIZE && height_tmp < REQUIRED_SIZE) 
       break; 
      width_tmp /= 2; 
      height_tmp /= 2; 
      scale *= 2; 
     } 

解碼與inSampleSize

BitmapFactory.Options o2 = new BitmapFactory.Options(); 
o2.inSampleSize = scale; 
bitmap = BitmapFactory.decodeFile(filePath, o2); 

imgView.setImageBitmap(bitmap); 

此外,如果你要上傳的全尺寸圖像,然後,只需使用文件路徑。

entity.addPart("uploaded", new FileBody(new File(filepath)));