三级缓存
缓存(内存)--->本地磁盘---->网络

1、首先看一下图片存储到本地磁盘
 1 public class FileUtils {
 2 
 3     String path;//文件存储的地方
 4     public FileUtils(Context context,String dirName){//文件夹的名称
 5 
 6         if(Environment.getExternalStorageState() == Environment.MEDIA_MOUNTED){//SD卡就绪
 7             //文件可以放在SD卡中
 8             path = Environment.getExternalStorageDirectory().getAbsolutePath()+"/"+dirName;
 9         }else{
10             //文件放在内部存储器里
11             path = context.getCacheDir().getAbsolutePath()+"/"+dirName;
12         }
13 
14         new File(path).mkdirs();
15     }
16 
17     //读取和写入
18     public void saveToSDCard(String key, Bitmap bmp){
19         FileOutputStream fos = null;
20         try {
21             fos = new FileOutputStream(new File(path,key));
22 
23         } catch (FileNotFoundException e) {
24             e.printStackTrace();
25         }
26         //保存固定格式图片,不压缩
27         bmp.compress(Bitmap.CompressFormat.PNG,100,fos);
28 
29         try {
30             fos.close();
31         } catch (IOException e) {
32             e.printStackTrace();
33         }
34     }
35     //读取
36     public Bitmap readFromSDCard(String key){
37         return BitmapFactory.decodeFile(new File(path,key).getAbsolutePath());
38     }
39 }
FileUtils.java

相关文章:

  • 2021-10-28
  • 2021-09-24
  • 2022-12-23
  • 2022-12-23
  • 2021-09-16
  • 2022-12-23
  • 2021-05-24
  • 2022-12-23
猜你喜欢
  • 2021-10-18
  • 2021-08-08
  • 2022-12-23
  • 2021-08-06
  • 2022-01-11
  • 2021-04-25
相关资源
相似解决方案