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:运行结果如下: