标签:
第一次连接网络下载图片后,将在本机/data/data/...下保存图片;第二次打开应用程序点击“获取图片”将直接从本机缓存获取。
缓存图片保存位置如下:
MainActivity.java
1 package com.example.day10_02getpicturebycache; 2 3 import java.io.File; 4 import java.io.FileOutputStream; 5 import java.io.InputStream; 6 import java.net.HttpURLConnection; 7 import java.net.URL; 8 9 10 11 import android.app.Activity; 12 import android.graphics.Bitmap; 13 import android.graphics.BitmapFactory; 14 import android.os.Bundle; 15 import android.os.Handler; 16 import android.os.Message; 17 import android.view.Menu; 18 import android.view.MenuItem; 19 import android.view.View; 20 import android.widget.ImageView; 21 import android.widget.Toast; 22 23 public class MainActivity extends Activity { 24 @Override 25 protected void onCreate(Bundle savedInstanceState) { 26 super.onCreate(savedInstanceState); 27 setContentView(R.layout.activity_main); 28 } 29 30 final static int MSG_DOWNLOAD_OK =1; 31 final static int MSG_DOWNLOAD_ERROR =2; 32 33 Handler handler = new Handler(){ 34 @Override 35 public void handleMessage(Message msg) { 36 super.handleMessage(msg); 37 //针对message进行处理 38 switch (msg.what) { 39 case MSG_DOWNLOAD_OK: 40 ImageView iv= (ImageView) findViewById(R.id.iv_pic); 41 iv.setImageBitmap((Bitmap)msg.obj); 42 break; 43 case MSG_DOWNLOAD_ERROR: 44 Toast.makeText(MainActivity.this, "下载失败", 1).show(); 45 break; 46 default: 47 break; 48 } 49 } 50 }; 51 52 public void getpicture(View v){ 53 File file = new File(getCacheDir(), "zy.jpg"); 54 if(file.exists()) { 55 Bitmap bm =new BitmapFactory().decodeFile(file.getAbsolutePath()); 56 ImageView iv= (ImageView) findViewById(R.id.iv_pic); 57 iv.setImageBitmap(bm); 58 } 59 else { 60 //下面的整个代码是从网络url下载对应的数据 61 Thread tread = new Thread(){ 62 public void run() { 63 String path = "http://192.168.3.30:8080/tomcat.gif"; 64 try { 65 URL url = new URL(path); 66 HttpURLConnection conn = (HttpURLConnection) url.openConnection(); 67 //set paramter 3 68 conn.setReadTimeout(5000); 69 conn.setRequestMethod("GET"); 70 conn.setConnectTimeout(5000); 71 // pending; 72 conn.connect(); 73 74 if (conn.getResponseCode()==200) { 75 InputStream is = conn.getInputStream(); 76 //如何放到缓存? 77 File file = new File(getCacheDir(), "zy.jpg"); 78 FileOutputStream fos = new FileOutputStream(file); 79 byte[] b =new byte[1024]; 80 int len=0; 81 while ((len=is.read(b))!=-1) { 82 fos.write(b, 0, len); 83 } 84 is.close(); 85 fos.close(); 86 87 //从一个保存的cache文件中,去得到一个bitmap对象。 88 Bitmap bm = new BitmapFactory().decodeFile(file.getAbsolutePath()); 89 90 Message msg = handler.obtainMessage(); 91 msg.what = MSG_DOWNLOAD_OK; 92 msg.obj = bm; 93 handler.sendMessage(msg); 94 95 } 96 else { 97 Message msg = handler.obtainMessage(); 98 msg.what =MSG_DOWNLOAD_ERROR; 99 handler.sendMessage(msg); 100 //Toast.makeText(this, "下载失败", 1).show(); 101 } 102 }catch (Exception e) { 103 e.printStackTrace(); 104 } 105 }; 106 }; 107 tread.start(); 108 } 109 } 110 }
activity_main.xml
1 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 2 xmlns:tools="http://schemas.android.com/tools" 3 android:layout_width="match_parent" 4 android:layout_height="match_parent" 5 android:orientation="vertical" > 6 7 <TextView 8 android:layout_width="wrap_content" 9 android:layout_height="wrap_content" 10 android:text="hello_world" /> 11 <Button 12 android:layout_width="wrap_content" 13 android:layout_height="wrap_content" 14 android:text="获取图片" 15 android:onClick="getpicture"/> 16 <ImageView 17 android:id="@+id/iv_pic" 18 android:layout_width="wrap_content" 19 android:layout_height="wrap_content" 20 android:src="@drawable/ic_launcher" 21 ></ImageView> 22 </LinearLayout>
AndroidManifest.xml中权限设置:
1 <uses-permission android:name="android.permission.INTERNET"/>
标签:
原文地址:http://www.cnblogs.com/woodrow2015/p/4516653.html