2014-12-04 141 views
1

我的應用程序需要上傳多個圖像到服務器。我通過解碼Base64發送圖像。從Android上傳圖像到服務器 - 上傳圖像的僅部分

 Bitmap bitmapOrg = BitmapFactory.decodeFile(imagePath); 
     ByteArrayOutputStream bao = new ByteArrayOutputStream(); 
     bitmapOrg.compress(Bitmap.CompressFormat.JPEG, 90, bao); 
     byte [] ba = bao.toByteArray(); 
     ba1 = Base64.encodeBytes(ba); 

而且在PHP

 $path = "/".$filename.".jpeg"; 
     $buffer = base64_decode($base); 
     $handle = fopen($path, 'wb'); 
     $numbytes = fwrite($handle, $buffer); 
     fclose($handle); 
     ob_clean(); 
     flush(); 

現在我可以看到,有些圖片只有部分上傳。我只能看到圖像的最上面的幾位,並且在服務器中全部空白。什麼可能是錯的?使用Base64解碼有什麼缺點?

回答

-1

請使用此方法。我希望它對你有用。在你的代碼中,可能是圖像像素顯示如此之高,然後你無法在服務器中看到圖像。

/** 
    * encodes image to string using Base64 
    * 
    * @return String imageString 
    */ 
    ** 

private String prepareImage() { 
     if (tempPath == null || (!isProfilechanged)) { 
      return ""; 
     } 
     BitmapFactory.Options options = new BitmapFactory.Options(); 
     options.inPreferredConfig = Bitmap.Config.ARGB_8888; 
     Bitmap bitmap = BitmapFactory.decodeFile(tempPath, options); 
     bitmap = Bitmap.createScaledBitmap(bitmap, 50, 50, true); 
     ByteArrayOutputStream baos = new ByteArrayOutputStream(); 
     // bitmap.compress(Bitmap.CompressFormat.PNG, 50, baos); 
     bitmap.compress(Bitmap.CompressFormat.JPEG, 50, baos); 
     byte[] byteArray = baos.toByteArray(); 
     String imageString = com.qwiches.utils.Base64.encodeBytes(byteArray); 
     bitmap = null; 
     System.gc(); 
     Runtime.getRuntime().gc(); 
     return imageString; 
    } 

**

+0

注生成文件的校驗和檢查這個link,如果你需要高質量的圖像,這種方法對你來說效果不佳,因爲它會將圖像縮小爲50x50像素的圖像。 – 2014-12-04 03:50:49

+0

是的,因爲當圖片尺寸太高時,沒有比例尺的上傳服務器。 – dipali 2014-12-04 03:55:28

+0

如果OP不關心分辨率,那麼你的解決方案工作正常。如果他真的關心它,那麼你的工作就會失敗。這是我唯一的觀點。 – 2014-12-04 03:57:39

0

嘗試checksum的文件,以確保文件是完全上傳

您可以在Android的