安卓如何发送图片并保存URL

8

可能是重复问题:
在安卓中发送post数据

如何通过http post方式发送图像及表单数据,比如图像名称等,到指定的url,即一个aspx页面。


你为什么认为它们应该有所不同? - Reno
1个回答

19

检查此代码以发送带有标题、说明、名称等的图像。

    HttpClient httpClient = new DefaultHttpClient();
    HttpPost postRequest = new HttpPost("You Link");
    MultipartEntity reqEntity = new MultipartEntity(HttpMultipartMode.BROWSER_COMPATIBLE);
    reqEntity.addPart("name", new StringBody("Name"));
    reqEntity.addPart("Id", new StringBody("ID"));
    reqEntity.addPart("title",new StringBody("TITLE"));
    reqEntity.addPart("caption", new StringBody("Caption"));
    try{
        ByteArrayOutputStream bos = new ByteArrayOutputStream();
        bitmap.compress(CompressFormat.JPEG, 75, bos);
        byte[] data = bos.toByteArray();
        ByteArrayBody bab = new ByteArrayBody(data, "forest.jpg");
        reqEntity.addPart("picture", bab);
    }
    catch(Exception e){
        //Log.v("Exception in Image", ""+e);
        reqEntity.addPart("picture", new StringBody(""));
    }
    postRequest.setEntity(reqEntity);       
    HttpResponse response = httpClient.execute(postRequest);
    BufferedReader reader = new BufferedReader(new InputStreamReader(response.getEntity().getContent(), "UTF-8"));
    String sResponse;
    StringBuilder s = new StringBuilder();
    while ((sResponse = reader.readLine()) != null) {
        s = s.append(sResponse);
    }

bitmap是图像位图。


太好了。视频上传怎么样? - Paresh Mayani
+1 谢谢,伙计。你救了我的一天。再次感谢。 - Simon Dorociak
2
但这在Android Studio上并没有发生 @Venky - Rahul Verma
addpart不起作用? - Neeraj Mehta

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接