缓存网络图片

来源:互联网 发布:麦咖啡软件 编辑:程序博客网 时间:2024/06/15 19:56


<?xml version="1.0" encoding="utf-8"?><RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"    xmlns:tools="http://schemas.android.com/tools"    android:id="@+id/activity_main"    android:layout_width="match_parent"    android:layout_height="match_parent"    tools:context="com.bwie.exam013.MainActivity">    <LinearLayout        android:id="@+id/ll"        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:orientation="horizontal">        <Button            android:id="@+id/net_image"            android:layout_width="0dp"            android:layout_weight="1"            android:layout_height="wrap_content"            android:text="显示网络图片"/>        <Button            android:id="@+id/local_image"            android:layout_width="0dp"            android:layout_weight="1"            android:layout_height="wrap_content"            android:text="显示缓存图片"/>        <Button            android:id="@+id/delete"            android:layout_width="0dp"            android:layout_weight="1"            android:layout_height="wrap_content"            android:text="清除缓存"/>    </LinearLayout>    <ImageView        android:layout_below="@+id/ll"        android:id="@+id/image"        android:src="@mipmap/ic_launcher"        android:layout_width="match_parent"        android:layout_height="match_parent" /></RelativeLayout>

package com.bwie.exam013;import android.os.Bundle;import android.os.Environment;import android.support.v7.app.AppCompatActivity;import android.view.View;import android.widget.Button;import android.widget.ImageView;import com.lidroid.xutils.BitmapUtils;import com.lidroid.xutils.cache.FileNameGenerator;public class MainActivity extends AppCompatActivity implements View.OnClickListener {    private static final String image_url="http://dl.bizhi.sogou.com/images/2015/02/12/1087025.jpg";    private static final String fileName="cacheFile";    private BitmapUtils bitmapUtils;    private String cachePath;    private ImageView image;    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_main);        initView();        cachePath= Environment.getExternalStorageDirectory()+"/cacheFileDir";        bitmapUtils=new BitmapUtils(this,cachePath);        bitmapUtils.configDiskCacheFileNameGenerator(new FileNameGenerator() {            @Override            public String generate(String key) {                return fileName;            }        });}    private void initView() {        image = (ImageView) findViewById(R.id.image);        Button net_image = (Button) findViewById(R.id.net_image);        Button local_image = (Button) findViewById(R.id.local_image);        Button delete = (Button) findViewById(R.id.delete);        net_image.setOnClickListener(this);        local_image.setOnClickListener(this);        delete.setOnClickListener(this);    }    public void onClick(View v) {        switch (v.getId()){            case R.id.net_image:                bitmapUtils.display(image,image_url);                break;            case R.id.local_image:                bitmapUtils.display(image,cachePath+"/"+fileName);                break;            case R.id.delete:                bitmapUtils.getBitmapFileFromDiskCache(cachePath+"/"+fileName).delete();                break;        }    }}

0 0
原创粉丝点击