1

我有此代码用于将图像上传到服务器。首先,我从图库中选择一张图片,当我想上传它时,我将 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);
4

1 回答 1

0

使用以下功能

将图像的位图和上传图像的 url 传递给函数

    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();
}
于 2013-09-25T11:29:13.343 回答