从SD卡中选择多个图像并将其上载到服务器

时间:2015-05-12 07:54:36

标签: android

我的代码:

protected String doInBackground(HttpResponse... arg0)
{
    String result = "";         
    File file = new File(selectImages);
    try {
        HttpClient client = new DefaultHttpClient();
        String postURL = "http://192.168.1.11/api/review/add_image.php?";
        post = new HttpPost(postURL); 
        FileBody bin = new FileBody(file);
        totalSize = bin.getContentLength();
        MultiPartEntity reqEntity = new MultiPartEntity(HttpMultipartMode.BROWSER_COMPATIBLE, new ProgressListener()
        {
            public void transferred(long num)
            {
                publishProgress((int) ((num / (float) totalSize) * 100));
            }
        });

        reqEntity.addPart("userfile", bin);
        post.setEntity(reqEntity); 
        HttpResponse response = client.execute(post);
        HttpEntity resEntity = response.getEntity();  
        if (resEntity != null) {    
            result = EntityUtils.toString(resEntity);
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
    return result;
}

我的结果为空。

1 个答案:

答案 0 :(得分:0)

这是将图像转换为base64的好方法。因此,您可以轻松发送更多图像。

imageView.buildDrawingCache();
Bitmap bm = imageView.getDrawingCache();
ByteArrayOutputStream baos = new ByteArrayOutputStream();  
bm.compress(Bitmap.CompressFormat.JPEG, 100, baos); //bm is the bitmap object   
byte[] b = baos.toByteArray();

String encodedImage = Base64.encodeToString(b , Base64.DEFAULT);

如果您有5张图片,可以将它们添加到阵列并将其发送到服务器。