【问题标题】:Android uploads black image to serverAndroid将黑色图像上传到服务器
【发布时间】:2025-12-26 14:00:12
【问题描述】:

我有此代码用于将图像上传到服务器。首先,我从图库中选择一张图片,当我想上传它时,我将 img 路径传递给异步任务,但在我没有错误地上传它之后,服务器显示 黑色图片。 我认为这与编码有关...

下面的代码是我上传图片的异步部分:

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);

【问题讨论】:

  • 这方面有什么更新吗?我遇到了同样的问题

标签: android image upload


【解决方案1】:

使用以下函数

将图片的位图和上传图片的网址传递给函数

    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();
}

【讨论】:

  • 仍然是黑色图像:S 我正在添加额外的 POST 参数,你认为这与它有什么关系吗?
  • 可能有一些服务器端脚本错误...因为我已经使用了很多次...并且图片上传正常
  • 你也可以试试这个coderzheaven.com/2012/03/29/…