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

软件发布|下载排行|最新软件

当前位置:首页IT学院IT技术

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

  2021-03-18 我要评论
想了解Android从服务器获取图片的实例方法的相关内容吗,在本文为您仔细讲解从服务器获取图片的相关知识和一些Code实例,欢迎阅读和指正,我们先划重点:服务器,获取,图片,下面大家一起来学习吧。

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

Copyright 2022 版权所有 软件发布 访问手机版

声明:所有软件和文章来自软件开发商或者作者 如有异议 请与本站联系 联系我们