Android中获取网络图片的方法(如果手机缓存里面有就从缓存获取)

来源:互联网 发布:心内事无人知谐音歌词 编辑:程序博客网 时间:2024/05/07 22:14
最近工作比较闲,除了用公司的imac机学学iphone外,有必要对以前的项目里面的难点进行一下总结了,对于Android开发中的难点,一是网络获取内容的处理,二是UI设计方面。对于我来说,特别麻烦就是UI设计方面的东西,公司的开发以iphone为主,毕竟香港人的iphone普及比较高(销售价格好像是全球最低的),为了模仿iphone的Tabbar,用TabActivity+ActivityGroup的处理方式不知道出了多少问题了,还好都一一解决了。
   获取网络图片的方法(如果手机缓存里面有就从缓存获取),我以前写的,比较原始:

Java代码  收藏代码
  1. ImageView mImageView = (ImageView)this.findViewById(R.id.imageview);  
  2. String imagePath = getImagePath(context, photoURL); // context:上下文 ,photoURL:图片的url路径  
  3. mImageView.setImageBitmap(BitmapFactory.decodeFile(imagePath));  


getImagePath()方法:
Java代码  收藏代码
  1. // 获取网络图片,如果缓存里面有就从缓存里面获取  
  2. public static  String getImagePath(Context context, String url) {  
  3.     if(url == null )  
  4.         return "";  
  5.     String imagePath = "";  
  6.     String   fileName   = "";  
  7.           
  8.     // 获取url中图片的文件名与后缀  
  9.     if(url!=null&&url.length()!=0){   
  10.         fileName  = url.substring(url.lastIndexOf("/")+1);  
  11.     }  
  12.       
  13.     // 图片在手机本地的存放路径,注意:fileName为空的情况  
  14.     imagePath = context.getCacheDir() + "/" + fileName;  
  15.     Log.i(TAG,"imagePath = " + imagePath);  
  16.     File file = new File(context.getCacheDir(),fileName);// 保存文件,  
  17.     if(!file.exists())  
  18.     {  
  19.         Log.i(TAG, "file 不存在 ");  
  20.         try {  
  21.             byte[] data =  readInputStream(getRequest(url));  
  22.             Bitmap bitmap = BitmapFactory.decodeByteArray(data, 0,  
  23.                     data.length);  
  24.               
  25.             bitmap.compress(CompressFormat.JPEG, 100new FileOutputStream(  
  26.                     file));  
  27.               
  28.             imagePath = file.getAbsolutePath();  
  29.             Log.i(TAG,"imagePath : file.getAbsolutePath() = " +  imagePath);  
  30.               
  31.         } catch (Exception e) {  
  32.             Log.e(TAG, e.toString());  
  33.         }  
  34.     }  
  35.     return imagePath;  
  36. // getImagePath( )结束。  


getRequest( ) 方法:网络获取图片为输入了
Java代码  收藏代码
  1. public static InputStream getRequest(String path) throws Exception{       
  2.     URL url = new URL(path);  
  3.         HttpURLConnection conn = (HttpURLConnection)url.openConnection();  
  4.         conn.setRequestMethod("GET");  
  5.         conn.setConnectTimeout(5000); // 5秒  
  6.         if(conn.getResponseCode() == 200){  
  7.             return conn.getInputStream();  
  8.         }  
  9.     return null;  
  10.   
  11. }  



readInputStream( ) 方法:把输入流转化成二进制

Java代码  收藏代码
  1. public static byte[] readInputStream(InputStream inStream) throws Exception{  
  2.         ByteArrayOutputStream outSteam = new ByteArrayOutputStream();  
  3.         byte[] buffer = new byte[4096];  
  4.         int len = 0;  
  5.         while( (len = inStream.read(buffer)) != -1 ){  
  6.             outSteam.write(buffer, 0, len);  
  7.         }  
  8.         outSteam.close();  
  9.         inStream.close();  
  10.         return outSteam.toByteArray();  
  11. }  


原创粉丝点击