欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  移动技术

Android编程实现图片的上传和下载功能示例

程序员文章站 2024-03-04 09:54:23
本文实例讲述了android编程实现图片的上传和下载功能。分享给大家供大家参考,具体如下: 在实现一个android的web服务客户端,比如微博,论坛客户端时,经常会使用...

本文实例讲述了android编程实现图片的上传和下载功能。分享给大家供大家参考,具体如下:

在实现一个android的web服务客户端,比如微博,论坛客户端时,经常会使用到图片的上传和下载。在这里介绍如何利用httpclient实现图片的上传和下载功能。

1 图片上传:上传图片时,首先获得图片的路径,创建文件,并将图片转化为字节流写入到request,并发送该请求。

客户端代码:

file file = new file(imageurl);
string httpurl = httpdomain+"addimageservlet"+"?gid="+gid;
httppost request = new httppost(httpurl);
httpclient httpclient = new defaulthttpclient();
fileentity entity = new fileentity(file,"binary/octet-stream");
httpresponse response;
try {
  request.setentity(entity);
  entity.setcontentencoding("binary/octet-stream");
  response = httpclient.execute(request);
//如果返回状态为200,获得返回的结果
if(response.getstatusline().getstatuscode()==httpstatus.sc_ok){
……//图片上传成功
}
}
catch(exception e){
}

服务器端所做的工作则是接收该字节流,写入文件中,并在服务器中相应文件夹中保存该文件,并记录该文件的路径,将图片文件路径写入到数据库中保存。

服务器端代码:

//获得新闻id
string gid = request.getparameter("gid");
string filepath = getrealpath(request) + "\\userpic\\";
//   定义上载文件的最大字节
int max_size = 102400 * 102400;
//   声明文件读入类
datainputstream in = null;
fileoutputstream fileout = null;
//   取得客户端上传的数据类型
string contenttype = request.getcontenttype();
if(contenttype.indexof("binary/octet-stream") >= 0){
  //   读入上传的数据
  in = new datainputstream(request.getinputstream());
  int formdatalength = request.getcontentlength();
  // 如果图片过大
  if(formdatalength > max_size){
    string errormsg=("上传的文件字节数不可以超过" + max_size);
    out.println(errormsg);
    return ;
  }
//   保存上传文件的数据
byte databytes[] = new byte[formdatalength];
int byteread = 0;
int totalbytesread = 0;
//   上传的数据保存在byte数组
while(totalbytesread < formdatalength){
byteread = in.read(databytes,totalbytesread,formdatalength);
totalbytesread += byteread;
 }
string filename = filepath + gid+".png";
 //   检查上载文件的目录是否存在
file filedir = new file(filepath);
if(!filedir.exists()){
filedir.mkdirs();
}
//   创建文件的写出类
fileout = new fileoutputstream(filename);
//   保存文件的数据
fileout.write(databytes);
fileout.close();
//保存文件的路径名
……

2 图片下载:首先获得网络图片的图片地址,发送请求后,服务器将会返回该图片的字节流,利用bitmapfactory.decodestream()方法将字节流转化为图片并返回。具体代码如下:

//获得网络中的图片
public bitmap getgossipimage(string gid){
    string httpurl = httpdomain+"userpic/"+gid+".png";
    bitmap bitmap = null;
    httpget httprequest = new httpget(httpurl);
    //取得httpclient 对象
    httpclient httpclient = new defaulthttpclient();
    try {
      //请求httpclient ,取得httprestponse
      httpresponse httpresponse = httpclient.execute(httprequest);
      if(httpresponse.getstatusline().getstatuscode() == httpstatus.sc_ok){
        //取得相关信息 取得httpentiy
        httpentity httpentity = httpresponse.getentity();
        inputstream is = httpentity.getcontent();
        bitmap = bitmapfactory.decodestream(is);
        is.close();
      }else{
         toast.maketext(context, "连接失败!", toast.length_short).show();
      }
    } catch (clientprotocolexception e) {
      e.printstacktrace();
    } catch (ioexception e) {
      e.printstacktrace();
    }
    return bitmap;
}

更多关于android相关内容感兴趣的读者可查看本站专题:《android图形与图像处理技巧总结》、《android开发入门与进阶教程》、《android调试技巧与常见问题解决方法汇总》、《android多媒体操作技巧汇总(音频,视频,录音等)》、《android基本组件用法总结》、《android视图view技巧总结》、《android布局layout技巧总结》及《android控件用法总结

希望本文所述对大家android程序设计有所帮助。