0

图片未使用 MultipartEntity 上传。

提供状态代码 200 但图像未在服务器端更新。

 String responseBody;
            HttpClient client = new DefaultHttpClient();
            HttpPost request = new HttpPost(
                    "http__zz/upload_picture?key=abc&property_id=10");

            MultipartEntity entity = new MultipartEntity(
                    HttpMultipartMode.BROWSER_COMPATIBLE);

            File file = new File(Environment.getExternalStoragePublicDirectory(
                    Environment.DIRECTORY_DCIM).toString()
                    + "/Camera/Test.jpg");
            ContentBody encFile = new FileBody(file, "image/png");

            entity.addPart("picture", encFile);

            request.setEntity(entity);

            ResponseHandler<String> responsehandler = new BasicResponseHandler();
            responseBody = client.execute(request, responsehandler);

            if (responseBody != null && responseBody.length() > 0) {
                Log.w("TAG", "Response image upload" + responseBody);

            }
4

3 回答 3

1

尝试使用 aByteArrayBody而不是 a FileBody

File file = new File(Environment.getExternalStoragePublicDirectory(
                    Environment.DIRECTORY_DCIM).toString()
                    + "/Camera/Test.jpg");
Bitmap b = BitmapFactory.decodeFile(file;
ByteArrayOutputStream bao = new ByteArrayOutputStream();
b.compress(CompressFormat.JPEG, 100, bao);

ByteArrayBody body = new ByteArrayBody(bao.toByteArray(), "image/jpeg", "picture");
entity.addPart("picture", body);
于 2013-09-26T13:22:44.833 回答
0

为什么不尝试将其作为 base64 编码字符串发送?

于 2013-09-26T13:22:23.540 回答
0

最好的方法是实现一个 IntentService 并使用广播意图通知状态。请从 git 中查看这段代码它对我的工作

https://github.com/alexbbb/android-upload-service

于 2015-05-09T13:07:18.503 回答