Android中获取网络图片的方法(如果手机缓存里面有就从缓存获取)
来源:互联网 发布:心内事无人知谐音歌词 编辑:程序博客网 时间:2024/05/07 22:14
最近工作比较闲,除了用公司的imac机学学iphone外,有必要对以前的项目里面的难点进行一下总结了,对于Android开发中的难点,一是网络获取内容的处理,二是UI设计方面。对于我来说,特别麻烦就是UI设计方面的东西,公司的开发以iphone为主,毕竟香港人的iphone普及比较高(销售价格好像是全球最低的),为了模仿iphone的Tabbar,用TabActivity+ActivityGroup的处理方式不知道出了多少问题了,还好都一一解决了。
获取网络图片的方法(如果手机缓存里面有就从缓存获取),我以前写的,比较原始:
getImagePath()方法:
getRequest( ) 方法:网络获取图片为输入了
readInputStream( ) 方法:把输入流转化成二进制
获取网络图片的方法(如果手机缓存里面有就从缓存获取),我以前写的,比较原始:
- ImageView mImageView = (ImageView)this.findViewById(R.id.imageview);
- String imagePath = getImagePath(context, photoURL); // context:上下文 ,photoURL:图片的url路径
- mImageView.setImageBitmap(BitmapFactory.decodeFile(imagePath));
getImagePath()方法:
- // 获取网络图片,如果缓存里面有就从缓存里面获取
- public static String getImagePath(Context context, String url) {
- if(url == null )
- return "";
- String imagePath = "";
- String fileName = "";
- // 获取url中图片的文件名与后缀
- if(url!=null&&url.length()!=0){
- fileName = url.substring(url.lastIndexOf("/")+1);
- }
- // 图片在手机本地的存放路径,注意:fileName为空的情况
- imagePath = context.getCacheDir() + "/" + fileName;
- Log.i(TAG,"imagePath = " + imagePath);
- File file = new File(context.getCacheDir(),fileName);// 保存文件,
- if(!file.exists())
- {
- Log.i(TAG, "file 不存在 ");
- try {
- byte[] data = readInputStream(getRequest(url));
- Bitmap bitmap = BitmapFactory.decodeByteArray(data, 0,
- data.length);
- bitmap.compress(CompressFormat.JPEG, 100, new FileOutputStream(
- file));
- imagePath = file.getAbsolutePath();
- Log.i(TAG,"imagePath : file.getAbsolutePath() = " + imagePath);
- } catch (Exception e) {
- Log.e(TAG, e.toString());
- }
- }
- return imagePath;
- } // getImagePath( )结束。
getRequest( ) 方法:网络获取图片为输入了
- public static InputStream getRequest(String path) throws Exception{
- URL url = new URL(path);
- HttpURLConnection conn = (HttpURLConnection)url.openConnection();
- conn.setRequestMethod("GET");
- conn.setConnectTimeout(5000); // 5秒
- if(conn.getResponseCode() == 200){
- return conn.getInputStream();
- }
- return null;
- }
readInputStream( ) 方法:把输入流转化成二进制
- public static byte[] readInputStream(InputStream inStream) throws Exception{
- ByteArrayOutputStream outSteam = new ByteArrayOutputStream();
- byte[] buffer = new byte[4096];
- int len = 0;
- while( (len = inStream.read(buffer)) != -1 ){
- outSteam.write(buffer, 0, len);
- }
- outSteam.close();
- inStream.close();
- return outSteam.toByteArray();
- }
- Android中获取网络图片的方法(如果手机缓存里面有就从缓存获取)
- Android中获取网络图片的方法(如果手机缓存里面有就从缓存获取)
- Android中获取网络图片的方法(如果手机缓存里面有就从缓存获取)
- Android中获取网络图片的方法(如果手机缓存里面有就从缓存获取)
- Android图片的三级缓存机制之从网络中获取图片
- Android图片的三级缓存机制之从网络中获取图片
- Android从网络获取图片,并缓存到SD卡
- Android 从WebView缓存中获取网页图片
- Android 从WebView缓存中获取网页图片
- Android 从WebView缓存中获取网页图片
- 从WebView缓存中获取网页图片
- 请求图片从缓存中获取的解决方案
- ReadWriteLock 数据先从缓存中去获取,如果缓存没有,则到数据库中获取
- 网络获取图片并缓存
- Android ListView从网络获取图片及文字显示---图片缓存,多线程处理
- 检测SDWebImage有没有缓存图片 IOS 获取网络图片大小
- 从网络获取图片,并缓存到SD卡
- 【Android网络编程】获取网络图片,具有缓存功能
- private inheritance是怎么回事,解释得比较到位
- openssl之stack学习
- csdn C论坛的学习记录
- 解决打开方式中无法添加zend studio 9.0问题
- Linux下切分Tomcat的catalina.out日志文件
- Android中获取网络图片的方法(如果手机缓存里面有就从缓存获取)
- Android网络连接Wifi和cmnet及cmwap的问题
- 各种语言的Hello,World程序大集合(一)
- android Service实例
- 今天遇到了路径返回html标记和服务器控件的区别
- Hive 导入导出数据
- iOS不敌Android的新证据
- .net七武器
- uCOS-II v2.52在MCS-51系列单片机上的移植实例(修订版)