将图像上传到android时出错

时间:2013-04-24 22:33:02

标签: android json image upload base64

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

我无法发布图片,请在此处查看图片: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);

非常感谢!

1 个答案:

答案 0 :(得分: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();
    }