android - 使用HTTP协议(protocol)将位图上传到服务器

标签 android

我有一个相机活动之后,我采取了一张照片,并保存到画廊和上传到服务器我的上传代码不工作,我需要在这方面的帮助?
//图像捕获

Intent intent = new Intent("android.media.action.IMAGE_CAPTURE");
startActivityForResult(intent, 0);

//图像保存
if(requestCode==0&&resultCode==RESULT_OK)

{

    Bundle extras = data.getExtras();
    Bitmap b = (Bitmap) extras.get("data");

    ImageView mImg;
    mImg = (ImageView) findViewById(R.id.head);
    mImg.setImageBitmap(b);

    // save image to gallery
    Shot = "HeadShot"; //Long.toString(System.currentTimeMillis());
    MediaStore.Images.Media.insertImage(getContentResolver(), b, Shot, NAME);

}

//上传到服务器
public void Upload(String url, HttpEntity imgdata) throws Exception, Exception {

    DefaultHttpClient client = new DefaultHttpClient();
    HttpPost post = new HttpPost(url);
    post.setHeader("Content-Type", "bitmap; charset=utf-8");
    post.setURI(new URI(url));
    post.setEntity(imgdata);
    HttpUriRequest request = post;
    HttpResponse response = client.execute(request);
    HttpEntity entity = response.getEntity();
    return entity.getContent();
}

最佳答案

这是一个很好的上传代码示例。它上传一个文件-正是你需要的How to send HTTP POST request and receive response?

关于android - 使用HTTP协议(protocol)将位图上传到服务器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3202627/

相关文章:

android - 专注于android中的第一个edtitext

javascript - 将下拉列表中的选择添加到列表

android - 如果Android版本低于3.0,如何隐藏操作栏(Theme.Holo)

Android 接口(interface)和 onCreate()

android - 错误 : The file name must end with . xml 或 .png

android - Observable 在 Kotlin 中列出

android - 如何在不使用 textview 的情况下从 Hashmap 获取一个值到 Intent ?

Android:提取列表中TextView的字符串

java - Jetty WebsocketClient 不连接 Android 上的 SSL

android - 从 Firebase 读取多个值并放置在折线图中