上传一个位图到服务器http协议 [英] Uploading a bitmap to the server with http protocol

查看:171
本文介绍了上传一个位图到服务器http协议的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我之后我拍张照片,并将其保存到相册,并上传到服务器我上传code不工作摄像头的活动,我需要帮助呢?

//图像捕获

 意向意图=新的意图(android.media.action.IMAGE_CAPTURE);
startActivityForResult(意向,0);
 

//保存图像

 如果(要求code == 0安培;&安培;结果code == RESULT_OK)

{

    捆绑额外= data.getExtras();
    位图B =(位图)extras.get(数据);

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

    //保存图像库
    射击=爆头; //Long.toString(System.currentTimeMillis的());
    MediaStore.Images.Media.insertImage(getContentResolver(),B,射击,名);

}
 

//上传到服务器

 公共无效上传(字符串URL,HttpEntity imgdata)抛出异常,异常{

    DefaultHttpClient客户端=新DefaultHttpClient();
    HttpPost后=新HttpPost(URL);
    post.setHeader(内容类型,位图;字符集= UTF-8);
    post.setURI(新的URI(URL));
    post.setEntity(imgdata);
    HttpUriRequest请求=职位;
    HTT presponse响应= client.execute(要求);
    HttpEntity实体= response.getEntity();
    返回entity.getContent();
}
 

解决方案

下面是一个不错的上传code样本。它上传文件 - 这正是你所需要的<一个href="http://stackoverflow.com/questions/3038409/how-to-send-http-post-request-and-recieve-response/3038747#3038747">http://stackoverflow.com/questions/3038409/how-to-send-http-post-request-and-recieve-response/3038747#3038747.

i have a camera Activity after which i take a picture and saving it to gallery and uploading to the server My upload code is not working, i need help on this?

// image capture

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

//image Saving

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);

}

//Upload to the server

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();
}

解决方案

Here's a nice upload code sample. It uploads a file - exactly what you need http://stackoverflow.com/questions/3038409/how-to-send-http-post-request-and-recieve-response/3038747#3038747.

这篇关于上传一个位图到服务器http协议的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆