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

Android从服务器获取图片的实例方法

程序员文章站 2023-12-03 14:37:22
[java]复制代码 代码如下:public static bitmap getbitmapfromserver(string imagepath) {  &nb...

[java]

复制代码 代码如下:

public static bitmap getbitmapfromserver(string imagepath) {

    httpget get = new httpget(imagepath);
    httpclient client = new defaulthttpclient();
    bitmap pic = null;
    try {
        httpresponse response = client.execute(get);
        httpentity entity = response.getentity();
        inputstream is = entity.getcontent();

        pic = bitmapfactory.decodestream(is);   // 关键是这句代码 

    } catch (clientprotocolexception e) {
        e.printstacktrace();
    } catch (ioexception e) {
        e.printstacktrace();
    }
    return pic;
}

 public static bitmap getbitmapfromserver(string imagepath) {

  httpget get = new httpget(imagepath);
  httpclient client = new defaulthttpclient();
  bitmap pic = null;
  try {
   httpresponse response = client.execute(get);
   httpentity entity = response.getentity();
   inputstream is = entity.getcontent();

   pic = bitmapfactory.decodestream(is);   // 关键是这句代码

  } catch (clientprotocolexception e) {
   e.printstacktrace();
  } catch (ioexception e) {
   e.printstacktrace();
  }
  return pic;
 }


其中imagepath是你的图片路径,


最后可以将图片显示在手机上:


[java]

复制代码 代码如下:

imageview.setimagebitmap(bitmap);