-1

我嘗試上傳圖像到服務器。我通過路徑得到的圖像並將其轉換爲字節數組:尺寸偏小圖像上傳

BitmapFactory.Options bmOptions = new BitmapFactory.Options(); 
Bitmap picture = BitmapFactory.decodeFile(imagePath, bmOptions); 

MultipartEntityBuilder builder = MultipartEntityBuilder.create(); 
ByteArrayOutputStream bao = new ByteArrayOutputStream(); 

bitmap.compress(Bitmap.CompressFormat.JPEG, 100, bao); 
byte[] bytes = bao.toByteArray(); 

builder.addPart("uploadedfile", new ByteArrayBody(bytes, "name" + ".jpg")); 

例如圖像的大小爲300KB,但上傳的圖片的大小爲800KB。

如何在不增加尺寸的情況下發送圖像(通過路徑選擇)?右


SOULUTION

@greenapps。我將圖像轉換爲文件:

public static byte[] fullyReadFileToBytes(File f) throws IOException { 
    int size = (int) f.length(); 
    byte bytes[] = new byte[size]; 
    byte tmpBuff[] = new byte[size]; 
    FileInputStream fis= new FileInputStream(f);; 
    try { 

     int read = fis.read(bytes, 0, size); 
     if (read < size) { 
      int remain = size - read; 
      while (remain > 0) { 
       read = fis.read(tmpBuff, 0, remain); 
       System.arraycopy(tmpBuff, 0, bytes, size - remain, read); 
       remain -= read; 
      } 
     } 
    } catch (IOException e){ 
     throw e; 
    } finally { 
     fis.close(); 
    } 

    return bytes; 
} 
+0

您是否嘗試將圖像作爲64位字符串編碼發送? – Eenvincible

+1

將圖像文件放在字節數組中而不使用中間位圖。 – greenapps

+0

@greenapps您可以添加此評論作爲答案。 –

回答

1

將圖像文件放入字節數組而不使用中間位圖。