【问题标题】:Use Base64OutputStream to Upload String to Server - Android使用 Base64OutputStream 将字符串上传到服务器 - Android
【发布时间】:2012-06-19 21:18:11
【问题描述】:

我一直在使用这段代码,但是遇到了一些内存问题:

      // Get the image from the sdcard
      Bitmap bm = BitmapFactory.decodeFile("/sdcard/myimage.jpg");
      // turn image into byte array output stream
      ByteArrayOutputStream baos = new ByteArrayOutputStream();  
      // 'compress' the jpeg
      bm.compress(Bitmap.CompressFormat.JPEG, 100, baos);               
      // get byte[] array of the image        
      byte[] byteArray = baos.toByteArray();
      // turn image into base64 string        
      String encodedImage = Base64.encodeToString(byteArray, Base64.DEFAULT);
      // and base64 string to 'params' value pair        
      params.add(new BasicNameValuePair("userfile", encodedImage));

      try {
       HttpPost request = new HttpPost();

       String urlString = "http://www.example.com";
       request.setURI(new URI(urlString));

     if(params != null) {
        request.setEntity(new UrlEncodedFormEntity(params));
        HttpClient client = new DefaultHttpClient();
        client.execute(request);
     } // end if
} // end try

有人建议我应该使用 Base64OutputStream 而不是 Base64.encodeToString ,但是我没有成功使用 Base64OutputStream 输出可以上传到服务器的字符串。在 IMAGE 上使用 Base64OutputStream 的任何示例都会有很大帮助。

编辑

要使答案起作用,您需要在您的 Android 项目中添加两个文件:apache-mime4j-dom-0.7.2.jar 和 httpmime-4.1.3.jar;

您可以从http://james.apache.org/download.cgi#Apache_Mime4J 下载 apache-mime4j-dom-0.7.2.jar - 下载二进制文件,解压缩,然后找到 apache-mime4j-dom-0.7.2.jar 文件。

然后去http://grepcode.com/snapshot/repo1.maven.org/maven2/org.apache.httpcomponents/httpmime/4.1.3下载httpmime-4.1.3.jar

然后将这两个文件拖到 Eclipse 中的项目中。然后在 Eclipse 中,选择 Project > Properties。选择Properties Pop-up,选择Java Build Path。单击“库”选项卡(查找源 | 项目 | 库 | 订购和导出)。点击“添加罐子”,选择apache-mime4j-dom-0.7.2.jar和httpmime-4.1.3.jar;然后单击“订购和导出”选项卡。检查 apache-mime4j-dom-0.7.2.jar 和 httpmime-4.1.3.jar;然后关闭该弹出窗口并从 Eclipse 菜单中选择 Project > Clean。

【问题讨论】:

    标签: android base64 photo


    【解决方案1】:

    如果可能,您不应该对文件进行 base64 编码并在 URL 中发送它们,而是使用 MultiPart 文件上传:

    HttpPost post = new HttpPost(URL);
    HttpClient client = new DefaultHttpClient();
    httpclient.getParams().setParameter(CoreProtocolPNames.PROTOCOL_VERSION, HttpVersion.HTTP_1_1);
    MultipartEntity entity = new MultipartEntity( HttpMultipartMode.BROWSER_COMPATIBLE );
    
    // picture
    entity.addPart( "userfile", new FileBody( 
        new File( MyApp.getContext().getFilesDir(), "userfile.jpg" ),
        "image/jpeg")
    );
    
    entity.addPart( "blahblah", new StringBody( "blah" ));  // string value
    
    post.setEntity( entity );
    client.execute( post );
    

    【讨论】:

    • 你能告诉我从哪里得到 JAR 以便我可以使用 MultipartEntity 吗?我之前尝试过这个并下载了我可以从 apache 找到的所有内容,但似乎没有任何效果。
    • 我编辑了原始帖子以显示从何处获取 JAR 文件以及如何将它们添加到您的项目中。
    • @Chris:抱歉来晚了,你已经找到文件了。
    猜你喜欢
    • 2016-06-10
    • 1970-01-01
    • 2014-08-20
    • 2012-10-04
    • 2014-03-07
    • 1970-01-01
    • 2017-09-30
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多