Android 把从网络获取的图片缓存到内存中
1:activity_main.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"> <Button
android:id="@+id/btn_1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Button test1"/> <Button
android:layout_below="@id/btn_1"
android:id="@+id/btn_2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Button Intent Other Activity"/> <ImageView
android:layout_below="@id/btn_2"
android:id="@+id/img_view"
android:layout_width="match_parent"
android:layout_height="match_parent"/> </RelativeLayout>
2:HttpHelper.java
public class HttpHelper {
    //图片资源缓存
    private static Map<String,Bitmap>bitmapCache=new HashMap<String,Bitmap>();
    public static Bitmap getHttpBitmap(String url){
     //首先先从缓存中取数据
        Bitmap bitmap=bitmapCache.get(url);
        if(bitmap!=null){
        //如果取到就直接返回
            return bitmap;
        }
        try{
            URL myUrl=new URL(url);
            HttpURLConnection conn=(HttpURLConnection)myUrl.openConnection();
            conn.setDoInput(true);
            conn.connect();
            InputStream is=conn.getInputStream();
            bitmap=BitmapFactory.decodeStream(is);
            is.close();
        }catch(Exception e){
            e.printStackTrace();
        }
        if(bitmap!=null){
        //将获取到的图片缓存起来
            bitmapCache.put(url, bitmap);
        }
        return bitmap;
    }
}
3:MainActivity.java
public class MainActivity extends Activity {
    private Button btnTest1=null;
    private Button btnTest2=null;
    private ImageView imgView=null;
    private Bitmap bitmap=null;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initUI();
        btnTest1.setOnClickListener(new OnClickListener(){
            public void onClick(View view){
                new Thread(new GetImgThread()).start();
            }
        });
        btnTest2.setOnClickListener(new OnClickListener(){
            public void onClick(View view){
                Intent intent=new Intent(MainActivity.this,OtherActivity.class);
                startActivity(intent);
            }
        });
    }
    private void initUI(){
        btnTest1=(Button)findViewById(R.id.btn_1);
        btnTest2=(Button)findViewById(R.id.btn_2);
        imgView=(ImageView)findViewById(R.id.img_view);
    }
    Handler myHandler=new Handler(){
        public void handleMessage(Message msg){
            imgView.setImageBitmap(bitmap);
        }
    };
    class GetImgThread implements Runnable{
        public void run(){
            String url="http://upload.wikimedia.org/wikipedia/commons/thumb/e/ea/Hukou_Waterfall.jpg/800px-Hukou_Waterfall.jpg";
            bitmap=HttpHelper.getHttpBitmap(url);
            myHandler.obtainMessage().sendToTarget();
        }
    }
}
4:activity_other.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent" > <Button
android:id="@+id/btn_get"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Button Get Img"/> <ImageView
android:id="@+id/img_view_2"
android:layout_below="@id/btn_get"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
</RelativeLayout>
5:OtherActivity.java
public class OtherActivity extends Activity {
    private  Bitmap bitmap=null;
    private Button btnGetImg=null;
    private ImageView imgView=null;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_other);
        btnGetImg=(Button)findViewById(R.id.btn_get);
        imgView=(ImageView)findViewById(R.id.img_view_2);
        btnGetImg.setOnClickListener(new OnClickListener(){
            public void onClick(View view){
                new Thread(new GetImgThread()).start();
            }
        });
    }
    Handler myHandler=new Handler(){
        public void handleMessage(Message msg){
            imgView.setImageBitmap(bitmap);
        }
    };
    class GetImgThread implements Runnable{
        public void run(){
            String url="http://upload.wikimedia.org/wikipedia/commons/thumb/e/ea/Hukou_Waterfall.jpg/800px-Hukou_Waterfall.jpg";
            bitmap=HttpHelper.getHttpBitmap(url);
            myHandler.obtainMessage().sendToTarget();
        }
    }
}
6:运行结果如下:


Android 把从网络获取的图片缓存到内存中的更多相关文章
- Android 将从网络获取的数据缓存到私有文件
		1:activity_main.xml <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/androi ... 
- Android热身:通过网络获取资源并更新UI组件
		Android热身:通过网络获取资源并更新UI组件 目标 点击"发送请求"按钮,下载某网页的html源码,并显示在TextView控件上:点击"清空",清除Te ... 
- iOS网络加载图片缓存策略之ASIDownloadCache缓存优化
		iOS网络加载图片缓存策略之ASIDownloadCache缓存优化 在我们实际工程中,很多情况需要从网络上加载图片,然后将图片在imageview中显示出来,但每次都要从网络上请求,会严重影响用 ... 
- oracle中如何将表缓存到内存中
		oracle快速将表缓存到内存中,使得访问速度加快. 共有2种方法: 1)alter table fisher cache; 2)alter table fisher storage(buffer ... 
- Android LazyList 从网络获取图片并缓存
		原演示地址 本文内容 环境 演示 LazyList 从网络获取图片并缓存 参考资料 本文是 Github 上的一个演示,通过网络获取歌手专辑的缩略图,并显示在 ListView 控件中.该演示具备将缩 ... 
- Android ListView从网络获取图片及文字显示
		上一篇文章说的是ListView展示本地的图片以及文本,这一篇说一下如何从网络获取图片以及文本来显示.事实上,一般是先获取Josn或sml数据,然后解释显示.我们先从网上获取xml,然后对其进行解析, ... 
- LruCache:从网络加载图片缓存实例
		OOM异常 堆内存用于存储实例对象,当程序不断创建对象,并且对象都有引用指向,那么垃圾回收机制就不会清理这些对象,当对象多到挤满堆内存的上限后,就产生OOM异常.Android系统为每个应用程序使用的 ... 
- Android开源项目发现--- 工具类图片缓存篇(持续更新)
		1. Android-Universal-Image-Loader 图片缓存 目前使用最广泛的图片缓存,支持主流图片缓存的绝大多数特性. 项目地址:https://github.com/nostra1 ... 
- 图片_ _图片缓存之内存缓存技术LruCache,软引用
		每当碰到一些大图片的时候,我们如果不对图片进行处理就会报OOM异常,这个问题曾经让我觉得很烦恼,后来终于得到了解决,那么现在就让我和大家一起分享一下吧.这篇博文要讲的图片缓存机制,我接触到的有两钟,一 ... 
随机推荐
- [Design Pattern] Flywight Pattern 简单案例
			Flywight Pattern, 即享元模式,用于减少对象的创建,降低内存的占用,属于结构类的设计模式.根据名字,我也将其会理解为 轻量模式. 下面是享元模式的一个简单案例. 享元模式,主要是重用已 ... 
- Java 中 MongoDB 使用指南
			一.引入MongoDB Java Driver包 如果需要操作MongoDB的Java项目是一个Maven项目,可以在依赖中加上以下的配置. <dependencies> <depe ... 
- HTTP学习笔记3-响应结构
			HTTP响应: 13,在接收和解释请求消息后,服务器会返回一个HTTP响应消息. 14,与HTTP请求类似,HTTP响应也是由三个部分组成,分别是:状态行.消息报头.响应正文. 15,状态行由协议版本 ... 
- MySQL中char、varchar和text的区别
			三者空间占用方面: char:存储定长数据很方便,CHAR字段上的索引效率极高,可以有默认值,比如定义char(10),那么不论你存储的数据是否达到了10个字节,都要占去10个字节的空间(自动用空格填 ... 
- 数据库安全之TDE列加密
			透明数据加密(Transparent Data Encryption) TDE - 基于列的加密 由于有了Oracle的TDE-基于列的加密,你所要做的只是定义需要加密的列,Oracle将为包含加密列 ... 
- Oracle Quality --- Setup Collection Element and Collection Plan
			Responsibility: Quality, Vision Enterprises 第一步: 创建 Collection Elements setup > collection elemen ... 
- Java经典23种设计模式之结构型模式(一)
			结构型模式包含7种:适配器模式.桥接模式.组合模式.装饰模式.外观模式.享元模式.代理模式. 本文主要介绍适配器模式和桥接模式. 一.适配器模式(Adapter) 适配器模式事实上非常easy.就像手 ... 
- uploadify上传大文件时出现404错误
			出现这个错误的话一般是IIs限制了文件大小.IIS7下的默认设置限制了上传大小.这个时候Web.Config中的大小设置也就失效了.具体步骤:1.打开IIS管理器,找到Default Web Site ... 
- 纯CSS3实现超立体的3D图片侧翻倾斜效果
			看到网友分享的一款CSS3 3D图片侧翻倾斜特效,觉得效果非常棒,其实话说回来,这玩意儿的实现真的非常简单,主要是创意不错.先来看看效果图.那么接下来我们分析一下源码吧,显示html代码,非常简单: ... 
- Android进程机制
			以下资料摘录整理自老罗的Android之旅博客,是对老罗的博客关于Android底层原理的一个抽象的知识概括总结(如有错误欢迎指出)(侵删):http://blog.csdn.net/luosheng ... 
