2013-09-25 39 views
1

我有這段代碼上傳圖片到服務器。首先,我從圖庫中選擇一張圖片,當我想上傳圖片時,我將img路徑傳遞給異步任務,但是在我沒有錯誤地上傳圖片後,服務器會顯示黑色圖片。 我認爲它是與編碼...Android上傳黑色圖片到服務器

下面的代碼是我的異步的一部分上傳圖片:

Bitmap bitmapOrg = BitmapFactory.decodeFile(IMG_PATH); ByteArrayOutputStream bao = new ByteArrayOutputStream();

   bitmapOrg.compress(Bitmap.CompressFormat.PNG, 100, bao); 


       byte[] data = bao.toByteArray(); 


       HttpClient httpClient = new DefaultHttpClient(); 
       HttpPost postRequest = new HttpPost(SERVER_PATH); 

       //filename 
       String fileName = String.format("File_%d.png",new Date().getTime()); 

       ByteArrayBody bab = new ByteArrayBody(data, fileName); 

       MultipartEntity reqEntity = new MultipartEntity(
         HttpMultipartMode.BROWSER_COMPATIBLE); 

       //POST params 
       reqEntity.addPart("image", bab); 
       reqEntity.addPart("user_id", new StringBody("123")); 
       reqEntity.addPart("apptoken", new StringBody("abcd123")); 

       Log.e("Response params", reqEntity.toString()); 

       postRequest.setEntity(reqEntity); 

       int timeoutConnection = 60000; 
       HttpParams httpParameters = new BasicHttpParams(); 
       HttpConnectionParams.setConnectionTimeout(httpParameters, 
         timeoutConnection); 
       int timeoutSocket = 60000; 
       HttpConnectionParams.setSoTimeout(httpParameters, timeoutSocket); 
       HttpConnectionParams.setTcpNoDelay(httpParameters, true); 

       HttpResponse response = httpClient.execute(postRequest); 

       BufferedReader reader = new BufferedReader(new InputStreamReader(

         response.getEntity().getContent(), "UTF-8")); 

       String sResponse; 

       StringBuilder s = new StringBuilder(); 

       while ((sResponse = reader.readLine()) != null) { 

        s = s.append(sResponse); 

       } 

       System.out.println("Response: " + s); 

回答

0

使用下列功能

將圖片的位圖和上傳圖片的網址傳遞給函數

private String uploadSlike(Bitmap bitmapOrg, String url) { 
    String sponse = null; 
    InputStream is; 
    try { 

     ByteArrayOutputStream bao = new ByteArrayOutputStream(); 

     bitmapOrg.compress(Bitmap.CompressFormat.PNG, 90, bao); 

     byte[] ba = bao.toByteArray(); 

     String ba1 = Base64.encodeBytes(ba); 

     ArrayList<NameValuePair> nameValuePairs = new 

     ArrayList<NameValuePair>(); 

     nameValuePairs.add(new BasicNameValuePair("imgdata", ba1)); 

     HttpClient httpclient = new DefaultHttpClient(); 

     HttpPost httppost = new 

     HttpPost(url); 

     httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs)); 

     HttpResponse response = httpclient.execute(httppost); 

     HttpEntity entity = response.getEntity(); 

     is = entity.getContent(); 
     sponse = convertStreamToString(is); 
     // String message=convertResponseToString(response); 

    } catch (Exception e) { 

    } 

    return sponse; 

} 

public static String convertStreamToString(InputStream is) throws Exception { 
    BufferedReader reader = new BufferedReader(new InputStreamReader(is)); 
    StringBuilder sb = new StringBuilder(); 
    String line = null; 

    while ((line = reader.readLine()) != null) { 
     sb.append(line); 
    } 

    is.close(); 

    return sb.toString(); 
} 
+0

仍在創建一個黑色圖像:S我添加了額外的POST參數,你認爲這有什麼用嗎? – Arthur

+0

有可能是一些服務器端腳本錯誤...因爲我用了很多次...和圖像上傳罰款 – WISHY

+0

你也可以試試這個http://www.coderzheaven.com/2012/03/29/上傳音頻,視頻或圖像,文件,從功能的Android到服務器/ – WISHY