0

我正在尝试使用 JSON+Base64 将图像从 android 上传到服务器。这在我使用模拟器时有效。但是当我使用手机时,我的图像丢失了..请帮助

我无法发布图片,请在此处查看图片:http: //i.upanh.com/rtnezbhttp://i.upanh.com/rtneqi

这是我的代码:

photo = BitmapFactory.decodeResource(getResources(), R.drawable.nocamera);
ByteArrayOutputStream baos = new ByteArrayOutputStream();       
photo.compress(Bitmap.CompressFormat.PNG, 100, baos);   
imageInByte = baos.toByteArray();
ImageBase64=Base64.encodeBytes(imageupdate);

非常感谢!

4

1 回答 1

1

尝试使用 Mutipart 实体上传图像文件。哟必须下载“httpmime-4.2.3.jar”库,这是我的一个项目中完美运行的示例代码。我希望这可以帮助你。

    DefaultHttpClient httpClient = new DefaultHttpClient();
    InputStream is = null;
    String json = "";
    JSONObject jObj;

    try {
        HttpPost httpPost = new HttpPost(url);
        MultipartEntity entity = new MultipartEntity(HttpMultipartMode.BROWSER_COMPATIBLE);

        File img = new File("local_image_path_here");
        entity.addPart("param_name_here", new FileBody(img));

        httpPost.setEntity(entity);

        HttpResponse httpResponse = httpClient.execute(httpPost);

        HttpEntity httpEntity = httpResponse.getEntity();
        is = httpEntity.getContent();

    } 
    catch (Exception e) {
        e.printStackTrace();
    }
于 2014-04-25T01:39:11.500 回答