Bitmap三级缓存+二次采样
*1.为什么Bitmap三级缓存?
没有缓存的弊端 :费流量, 加载速度慢
加入缓存的优点: 省流量,支持离线浏览
从内存获取图片, 如果存在, 则显示; 如果不存在, 则从SD卡中获取图片
从SD卡中获取图片, 如果文件中存在, 显示, 并且添加到内存中; 否则开启网络下载图片
从网络下载图片, 如果下载成功, 则添加到缓存中, 存入SD卡, 显示图片
网络权限
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"></uses-permission>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"></uses-permission>
<uses-permission android:name="android.permission.INTERNET"></uses-permission>
工具类 内存版
package com.example.day6;
import android.graphics.Bitmap;
import android.util.LruCache;
public class neicun {
static long max = Runtime.getRuntime().maxMemory();
static LruCache<String, Bitmap> lruCache = new LruCache<String, Bitmap>((int) max / 8) {
@Override
protected int sizeOf(String key, Bitmap value) {
return value.getByteCount();
}
};
public static void getBitmap(String key,Bitmap bitmap){
lruCache.put(key,bitmap);
}
public static Bitmap getbitmap(String key){
return lruCache.get(key);
}
}
工具类 网络版
package com.example.day6;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.AsyncTask;
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
import java.util.concurrent.ExecutionException;
public class net {
public static Bitmap getBitmap(String url){
Bitmap bitmap=null;
try {
bitmap= new Mytask().execute(url).get();
} catch (ExecutionException e) {
e.printStackTrace();
} catch (InterruptedException e) {
e.printStackTrace();
}
return bitmap;
}
static class Mytask extends AsyncTask<String,String,Bitmap>{
@Override
protected Bitmap doInBackground(String... strings) {
try {
URL url = new URL(strings[0]);
HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();
urlConnection.connect();
InputStream inputStream = urlConnection.getInputStream();
Bitmap bitmap = BitmapFactory.decodeStream(inputStream);
return bitmap;
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
}
}
工具类 sd卡版
package com.example.day6;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Environment;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
public class sdka {
public static void setbitmap(String file, Bitmap bitmap){
if(Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)){
try {
bitmap.compress(Bitmap.CompressFormat.JPEG,100,new FileOutputStream(file));
} catch (FileNotFoundException e) {
e.printStackTrace();
}
}
}
public static Bitmap getbitmap(String string){
if(Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)){
Bitmap bitmap = BitmapFactory.decodeFile(string);
return bitmap;
}
return null;
}
}
二级采样
为什么要使用二级采样
BitmapFactory.Options getbitmap= new BitmapFactory.Options();
getbitmap.inJustDecodeBounds=true;
BitmapFactory.decodeFile(s,getbitmap);
int outWidth = getbitmap.outWidth;
int outHeight = getbitmap.outHeight;
int size=1;
while (outHeight/size>200||outWidth/size>200){
size*=2;
}
getbitmap.inJustDecodeBounds=false;
getbitmap.inSampleSize=size;
Bitmap bitmap = BitmapFactory.decodeFile(s,getbitmap);
三级缓存
if(ca==null){
Bitmap getbitmap = sdka.getbitmap("/sdcard/Pictures/ca.jpg");
if(getbitmap==null){
Bitmap bitmap = net.getBitmap("http://pic1.win4000.com/wallpaper/e/50d80458e1373.jpg");
if(bitmap==null){
Toast.makeText(MainActivity.this, "没网", Toast.LENGTH_SHORT).show();
}else{
imageView.setImageBitmap(bitmap);
neicun.getBitmap("ca",bitmap);
sdka.setbitmap("/sdcard/Pictures/ca.jpg",bitmap);
Toast.makeText(MainActivity.this, "网络", Toast.LENGTH_SHORT).show();
}
}else{
imageView.setImageBitmap(getbitmap);
neicun.getBitmap("ca",getbitmap);
Toast.makeText(MainActivity.this, "SD", Toast.LENGTH_SHORT).show();
}
}else{
imageView.setImageBitmap(ca);
Toast.makeText(MainActivity.this, "内存", Toast.LENGTH_SHORT).show();
}
压缩质量
其中有一个 int类型的参数 他表示的是质量 0-100
bitmap.compress(Bitmap.CompressFormat.JPEG,100,new FileOutputStream("/sdcard/pictures/what.jpg"));