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);
仍在創建一個黑色圖像:S我添加了額外的POST參數,你認爲這有什麼用嗎? – Arthur
有可能是一些服務器端腳本錯誤...因爲我用了很多次...和圖像上傳罰款 – WISHY
你也可以試試這個http://www.coderzheaven.com/2012/03/29/上傳音頻,視頻或圖像,文件,從功能的Android到服務器/ – WISHY