您的位置:首页 > 其它

内部存储 SD卡存储 三级缓存

2019-05-14 21:52 411 查看

内部存储 SD卡存储 三级缓存

内部存储

//可存储多种类型数据
//参数一 xml文件的名字 参数二 模式 MODE_PRIVATE 指定该SharedPreferences数据只能被本应用程序读写
SharedPreferences sharedPreferences = getSharedPreferences("qq.txt", MODE_PRIVATE);
SharedPreferences.Editor editor = sharedPreferences.edit();
editor.putString("username",s1);
editor.putString("password",s2);
editor.commit();

存储位置在 data/data/包名/shared_prefs 目录下 随着app的卸载消失

SD卡存储

首先设置权限

代码: 存储位置 mnt/shell/emulated/0 目录下

if(Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)){
File file = Environment.getExternalStorageDirectory();
try {
//写入数据
FileOutputStream fos = new FileOutputStream(new File(file, "我丢丢"));
fos.write("我丢我丢丢".getBytes());
fos.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}

}

//读取数据
if(Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)){
File file = Environment.getExternalStorageDirectory();
try {
FileInputStream fis = new FileInputStream(new File(file, "我丢丢"));
byte[] b = new byte[1024];
int read = fis.read(b);
String s = new String(b, 0, read);
fis.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}

}

三级缓存

先写入三个工具类
别忘了添加权限

public class Http {

static public Bitmap downLoadImage(String str) throws IOException {
URL url = new URL(str);
HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();
InputStream inputStream = urlConnection.getInputStream();
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
byte[] b= new byte[1024];
int len = -1;
while((len=inputStream.read(b))!=-1){
byteArrayOutputStream.write(b,0,len);
}
byte[] bytes = byteArrayOutputStream.toByteArray();
Bitmap bitmap = BitmapFactory.decodeByteArray(bytes, 0, bytes.length);

return bitmap;
}

}

```javascript
public class LruUtils {
//Lrucache存储工具类
//TODO 1:实例化LruCache对象
private LruCache<String,Bitmap> lruCache;
private long max=Runtime.getRuntime().maxMemory();//获得手机的最大内存
public LruUtils(){
lruCache=new LruCache<String,Bitmap>((int)max/8){//给内存大小,一般是最大内存的1/8
//重写该方法返回每个对象的大小
@Override
protected int sizeOf(String key, Bitmap value) {
return value.getByteCount();
}
};
}
public Bitmap getBitmap(String key){
Log.d("###", String.valueOf(lruCache.get(key)));
if(lruCache.get(key)==null){
return null;
}
return lruCache.get(key);
}

public  void setBitmap(String key, Bitmap bitmap){
lruCache.put(key,bitmap);
}
}
public class SDcar {

public static void setBitmap(String name, Bitmap bitmap){
//获取路径存储图片
if(Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)){
File file=Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES);
File file1=new File(file,name);
//存储图片:bitmap对象---->SD卡
try {
//参数一 图片的格式 参数二 图片质量 0-100  参数三:输出流
bitmap.compress(Bitmap.CompressFormat.JPEG,100,new FileOutputStream(file1));
} catch (FileNotFoundException e) {
e.printStackTrace();
}
}
}

public static Bitmap getBitmap(String name){
//获取路径存储图片
if(Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)){
File file=Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES);
File file1=new File(file,name);
//读取图片:SD卡-----Bitmap
if(!file1.exists()) {
return null;
}
return BitmapFactory.decodeFile(file1.getAbsolutePath());

}
return null;
}

}

实现代码 我们可以设置一个Button和ImageView 简单实现一下:

button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Bitmap aaa = lruUtils.getBitmap("aaa");
if(aaa==null){
Bitmap aaa1 = SDcar.getBitmap("aaa.jpg");
if(aaa1==null){
new Thread(new Runnable() {
@Override
public void run() {
try {
Bitmap bitmap = Http.downLoadImage("http://img.zcool.cn/community/01b078574706206ac72525ae0beaed.jpg@2o.jpg");
Log.d("###", String.valueOf(bitmap));
Message obtain = Message.obtain();
obtain.obj=bitmap;
handler.sendMessage(obtain);

} catch (IOException e) {
e.printStackTrace();
}
}
}).start();
}else {
imageView.setImageBitmap(aaa1);
lruUtils.setBitmap("aaa",aaa1);
}

}else {
imageView.setImageBitmap(aaa);
}

}
});
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: