1:activity_main.xml

Android 把从网络获取的图片缓存到内存中

<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. Android 的网络编程(17)-android显示网络图片
  2. androidSDK下的图片资源
  3. 自定义RadioButton 文字在下,图片在上
  4. 自定义progressbar使用图片
  5. Shape实现圆形图片
  6. Android 内存泄漏场景分析
  7. Android 异步加载图片
  8. android手机中图片的拖拉及浏览功能
  9. Android studio图片ERROR: 9-patch image xx .9.png malformed

随机推荐

  1. android如何在非context環境訪問資源
  2. Android Stuido Ndk-Jni 开发(二):Jni中打印
  3. android internet参数传递
  4. OpenCV android sdk配置OpenCV android N
  5. Android异步处理:AsyncTask的实现原理
  6. VS2015+Android环境配置【appt.exe停止运
  7. Android 关于 Activity 之间的切换动画
  8. Android Gradle上传Maven仓库
  9. android一些东东
  10. Android UC浏览器使用痕迹分析