1。之前因为做一个项目的过程中遇到要频繁重复下载的文件比如图片等,需要在本地缓存,除了用户体验也保证了省流量。
这个demo是用下载网络图片来演示。
一共有六张网络图片,加载图片时,会判断图片是否下载到本地的文件夹中,如果下载了,直接从文件中读取,如果没有就会去下载,下载完之后通过一个handler通知adapter的notifyDataSetChanged()方法,来对listActivity进行通知,来显示对应的列表。
具体代码如下;
public class CatheAdapter extends BaseAdapter {
private List<String> mUrl = new ArrayList<String>();
private LayoutInflater mInflater;
private Context context;
public CatheAdapter(Context context) {
mUrl.add("http://papercut.jd-app.com/learn/Goat_8/2.png");
mUrl.add("http://papercut.jd-app.com/learn/Goat_8/3.png");
mUrl.add("http://papercut.jd-app.com/learn/Goat_8/4.png");
mUrl.add("http://papercut.jd-app.com/learn/Goat_8/5.png");
mUrl.add("http://papercut.jd-app.com/learn/Goat_8/6.png");
mUrl.add("http://papercut.jd-app.com/learn/Goat_8/7.png");
this.context = context;
mInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
@Override
public int getCount() {
// TODO Auto-generated method stub
return mUrl.size();
}
@Override
public Object getItem(int position) {
// TODO Auto-generated method stub
return null;
}
@Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return 0;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
if (convertView == null) {
convertView = mInflater.inflate(R.layout.item,null);
}
ImageView image = (ImageView) convertView.findViewById(R.id.image);
image.setImageBitmap(getBitmap(position));
return convertView;
}
private Bitmap getBitmap(int position){
Bitmap bitmap = null;
//File dir = Environment.getExternalStorageDirectory();
String fileName = File.separator+"sdcard"+File.separator+mUrl.get(position).hashCode();
if (new File(fileName).exists()) {
bitmap = BitmapFactory.decodeFile(fileName);
}else {
bitmap = BitmapFactory.decodeResource(context.getResources(), R.drawable.ic_launcher);
new DownLoadImage(this, mUrl.get(position)).start();
}
return bitmap;
}
}
这是adaopter类的代码。
public class DownLoadImage extends Thread{
private CatheAdapter catheAdapter;
private String mUrl;
private String fileName;
private File dir;
private Handler handler = new Handler(){
@Override
public void handleMessage(Message msg) {
catheAdapter.notifyDataSetChanged();
}
};
public DownLoadImage(CatheAdapter catheAdapter, String mUrl) {
this.catheAdapter = catheAdapter;
this.mUrl = mUrl;
//dir = Environment.getExternalStorageDirectory();
fileName = File.separator+"sdcard"+File.separator+mUrl.hashCode();
}
@Override
public void run() {
URL url;
try {
url = new URL(mUrl);
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setDoInput(true);
conn.setDoOutput(true);
conn.setRequestMethod("GET");
conn.setUseCaches(false);
InputStream is = conn.getInputStream();
FileOutputStream fos = new FileOutputStream(fileName);
byte[] buffer = new byte[8192];
int count = 0;
while ((count = is.read(buffer))>-1) {
fos.write(buffer,0,count);
}
fos.flush();
fos.close();
is.close();
conn.disconnect();
handler.sendEmptyMessage(0);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
这是下载图片的类,图片放在了sdcard的下面,因为是演示所以这么写了。建议大家如果要新建一个文件。
最后是主要的类。
public class MainActivity extends ListActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
this.setListAdapter(new CatheAdapter(this));
}
}
布局代码如下:
<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"
tools:context="liu.example.cathetest.MainActivity" >
<ListView
android:id="@android:id/list"
android:layout_width="fill_parent"
android:layout_height="wrap_content"/>
</RelativeLayout>
这是每一个item的布局代码;
<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"
tools:context="liu.example.cathetest.MainActivity" >
<ListView
android:id="@android:id/list"
android:layout_width="fill_parent"
android:layout_height="wrap_content"/>
</RelativeLayout>
最后主要权限,除了Internet权限还有访问sdcard的权限。