从网络获取图片,并缓存到SD卡,flutter实时刷新

///创建缓存目录

cache=new File(Environment.getExternalStorageDirectory(),“cache”);

if(!cache.exists()){

cache.mkdirs();

}

}

public void skip(View v){

Intent intent=new Intent(this,MainActivity.class);

startActivity(intent);

}

public void upload(View v){

paths=new String[5];

for(int i=0;i<5;i++){

//图片路径

paths[i]=Environment.getExternalStorageDirectory()+"/"+i+".jpg";

}

//批量上传图片

Bmob.uploadBatch(this, paths, new UploadBatchListener() {

@Override

public void onSuccess(List arg0, List arg1) {

// TODO Auto-generated method stub

String strurl="";

for(String url:arg1){

strurl=url;

}

Person p=new Person();

p.setIcPath(strurl);

p.setStr(“s”+j);

p.save(IndexActivity.this);

j++;

toast(“第”+j+“个上传成功!”);

}

@Override

public void onProgress(int curIndex, int curPercent, int total, int totalPercent) {

// TODO Auto-generated method stub

//1、curIndex–表示当前第几个文件正在上传

//2、curPercent–表示当前上传文件的进度值(百分比)

//3、total–表示总的上传文件数

//4、totalPercent–表示总的上传进度(百分比)

MyUtil.logI(curIndex+" “+curPercent+” “+total+” "+totalPercent);

}

@Override

public void onError(int arg0, String arg1) {

// TODO Auto-generated method stub

toast("error "+arg1);

MyUtil.logI("errer "+arg1);

}

});

}

// 清理缓存

public void clear(View v){

if(cache.length()!=0){

File[] files=cache.listFiles();

for(File file:files){

file.delete();

}

cache.delete();

toast(“缓存清理成功”);

}else{

toast(“暂无缓存”);

}

}

public void toast(String msg){

Toast.makeText(this, msg, Toast.LENGTH_SHORT).show();

}

}

MainActivity

public class MainActivity extends Activity {

private ListView lv;

private MyAdapter adapter;

private File cache;

private List list;

protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_main);

initView();

}

private void initView() {

// TODO Auto-generated method stub

lv=(ListView)findViewById(R.id.listView1);

list=new ArrayList();

cache=new File(Environment.getExternalStorageDirectory(),“cache”);

if(!cache.exists()){

cache.mkdirs();

}

getData();

}

private void getData() {

// TODO Auto-generated method stub

//查询数据

BmobQuery bq=new BmobQuery();

bq.setCachePolicy(CachePolicy.CACHE_ELSE_NETWORK); //缓存查询

bq.findObjects(this, new FindListener() {

@Override

public void onSuccess(List arg0) {

// TODO Auto-generated method stub

list=arg0;

adapter=new MyAdapter(MainActivity.this, list, cache);

lv.setAdapter(adapter);

}

@Override

public void onError(int arg0, String arg1) {

// TODO Auto-generated method stub

MyUtil.logI("errer "+arg1);

}

});

}

MyAdapter

public class MyAdapter extends BaseAdapter {

private Context context;

private List list;

private File cache;

public MyAdapter(Context context, List list, File cache) {

this.context = context;

this.list = list;

this.cache = cache;

}

@Override

public int getCount() {

// TODO Auto-generated method stub

return list.size();

}

@Override

public Object getItem(int position) {

// TODO Auto-generated method stub

return list.get(position);

}

@Override

public long getItemId(int position) {

// TODO Auto-generated method stub

return position;

}

@Override

public View getView(int position, View convertView, ViewGroup parent) {

// TODO Auto-generated method stub

ViewHolder vh;

if(convertView==null){

convertView=LayoutInflater.from(context).inflate(R.layout.item, null);

vh=new ViewHolder();

vh.tv=(TextView)convertView.findViewById(R.id.tv);

vh.iv=(ImageView)convertView.findViewById(R.id.iv);

convertView.setTag(vh);

}else{

vh=(ViewHolder)convertView.getTag();

}

final Person p=list.get(position);

vh.tv.setText(p.getStr());

// 异步的加载图片

MyUtil mu=new MyUtil();

AsyncImageTask as=new AsyncImageTask(mu,vh.iv,cache);

if(p.getIcPath()!=null){

as.execute(p.getIcPath());

}else{

vh.iv.setImageResource(R.drawable.ic_launcher);

}

return convertView;

}

class ViewHolder{

TextView tv;

ImageView iv;

}

}

MyUtil

《Android学习笔记总结+最新移动架构视频+大厂安卓面试真题+项目实战源码讲义》

【docs.qq.com/doc/DSkNLaERkbnFoS0ZF】 完整内容开源分享

public class MyUtil {

/**

  • 从网络上获取图片,如果图片在本地存在的话就直接拿,如果不存在再去服务器上下载图片

  • */

public Uri getImageURI(String path,File cache) throws Exception{

String name=MD5.getMD5(path)+path.substring(path.lastIndexOf("."));

logI(“name”+name);

File file=new File(cache, name);

// 如果图片存在本地缓存目录,则不去服务器下载

if(file.exists()){

logI(“cache”);

return Uri.fromFile(file);

}else{

URL url=new URL(path);

HttpURLConnection conn=(HttpURLConnection)url.openConnection();

conn.setConnectTimeout(5000);

conn.setRequestMethod(“GET”);

conn.setDoInput(true);

if(conn.getResponseCode()==200){

InputStream is=conn.getInputStream();

FileOutputStream fos=new FileOutputStream(file);

byte[] b=new byte[2*1024];

int len=0;

while((len=is.read(b))!=-1){

fos.write(b,0,len);

你可能感兴趣的:(程序员,面试,android,移动开发)