Android读取服务器的图片-三种方法

 Android链接服务器获取图片在此提供三种方法,已通过验证,无误。

方法一:

[java]  view plain  copy
  1. public static Bitmap getImage(String path){  
  2.       
  3.     try {  
  4.         HttpURLConnection conn = (HttpURLConnection) new URL(path).openConnection();  
  5.         conn.setConnectTimeout(5000);  
  6.         conn.setRequestMethod("GET");  
  7.         System.out.println("tdw1");  
  8.         if(conn.getResponseCode() == 200){  
  9.             InputStream inputStream = conn.getInputStream();  
  10.             Bitmap bitmap = BitmapFactory.decodeStream(inputStream);     
  11.             return bitmap;  
  12.         }  
  13.     } catch (Exception e) {  
  14.         e.printStackTrace();  
  15.     }  
  16.     return null;  
  17. }  

在第一种方法中,从conn的输入流中获取数据将其转化为Bitmap型数据。

在功能代码中:

[java]  view plain  copy
  1. image.setImageBitmap(getImage("路径"));  
image为ImageView型控件。


第二种方法:

[java]  view plain  copy
  1. public static Bitmap getImage1(String path){  
  2.       
  3.         HttpGet get = new HttpGet(path);  
  4.         HttpClient client = new DefaultHttpClient();  
  5.         Bitmap pic = null;  
  6.           try {  
  7.            HttpResponse response = client.execute(get);  
  8.            HttpEntity entity = response.getEntity();  
  9.            InputStream is = entity.getContent();  
  10.   
  11.            pic = BitmapFactory.decodeStream(is);   // 关键是这句代  
  12.     } catch (Exception e) {  
  13.         e.printStackTrace();  
  14.     }  
  15.     return pic;  
  16. }  

这个方法类似上面那个方法。在功能代码中设置是一样的


第三种方法:

[java]  view plain  copy
  1. public static Uri getImage2(String path,File cacheDir){  
  2.         File localFile = new File(cacheDir,MD5.getMD5(path)+path.substring(path.lastIndexOf(".")));  
  3.         if(localFile.exists()){  
  4.             return Uri.fromFile(localFile);  
  5.         }else  
  6.         {  
  7.             HttpURLConnection conn;  
  8.             try {  
  9.                 conn = (HttpURLConnection) new URL(path).openConnection();  
  10.                 conn.setConnectTimeout(5000);  
  11.                 conn.setRequestMethod("GET");  
  12.                 if(conn.getResponseCode() == 200){  
  13.                     System.out.println("tdw");  
  14.                     FileOutputStream outputStream = new FileOutputStream(localFile);  
  15.                     InputStream inputStream = conn.getInputStream();  
  16.                     byte[] buffer = new byte[1024];  
  17.                     int length = 0;  
  18.                     while((length=inputStream.read(buffer))!=-1){  
  19.                         outputStream.write(buffer, 0, length);  
  20.                     }  
  21.                     inputStream.close();  
  22.                     outputStream.close();  
  23.                     return Uri.fromFile(localFile);  
  24.                 }  
  25.             } catch (Exception e) {  
  26.                 // TODO Auto-generated catch block  
  27.                 e.printStackTrace();  
  28.             }  
  29.         }  
  30.         return null;      
  31.     }  

第三种方法,将从服务器获取的数据存入本地的文件中,如果文件已存在,则不需要从服务器重新获取数据。

在功能代码中:

[java]  view plain  copy
  1. image.setImageURI(getImage2(path, cache));  

上面代码中设置图片为缓存设置,这样如果图片资源更新了,则需要重新命名文件的名字,这样才能够重新加载新图片。

[java]  view plain  copy
  1. cache = new File(Environment.getExternalStorageDirectory(),"cache");  
  2. if(!cache.exists()){  
  3.    cache.mkdirs();  
  4. }  

这里是设置 缓存图片的路径。

以上为三种方法。

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值