保存Volley的缓存图片到本地,当网络不通的时候,访问本地的图片

来源:互联网 发布:软件解锁功能 编辑:程序博客网 时间:2024/04/27 19:51

1.ComTools.java

       /**

* 保存图片到本地,这个是把图片压缩成字节流然后保存到本地,所以本地的图片是无法显示的

* @param mBitmap
* @param imageURL
* @param cxt
*/
public static void saveBitmap(Bitmap mBitmap, String imageURL, Context cxt) {


String bitmapName = imageURL.substring(imageURL.lastIndexOf("/") + 1);//传入一个远程图片的url,然后取最后的图片名字


ByteArrayOutputStream stream = new ByteArrayOutputStream();
mBitmap.compress(Bitmap.CompressFormat.PNG, 100, stream);
byte[] byteArray = stream.toByteArray();


FileOutputStream fos = null;
ObjectOutputStream oos = null;


try {
fos = cxt.openFileOutput(bitmapName, Context.MODE_PRIVATE);
oos = new ObjectOutputStream(fos);
oos.writeObject(byteArray);
} catch (Exception e) {
e.printStackTrace();
// 这里是保存文件产生异常
} finally {
if (fos != null) {
try {
fos.close();
} catch (IOException e) {
// fos流关闭异常
e.printStackTrace();
}
}
if (oos != null) {
try {
oos.close();
} catch (IOException e) {
// oos流关闭异常
e.printStackTrace();
}
}
}
}


/**
* 读取本地私有文件夹的图片

* @param name
* @param cxt
* @return
*/
public static Bitmap getBitmap(String fileName, Context cxt) {
String bitmapName = fileName.substring(fileName.lastIndexOf("/") + 1);
FileInputStream fis = null;
ObjectInputStream ois = null;
try {
fis = cxt.openFileInput(bitmapName);
ois = new ObjectInputStream(fis);
byte[] byteArray = (byte[]) ois.readObject();
Bitmap bitmap = BitmapFactory.decodeByteArray(byteArray, 0,
byteArray.length);
return bitmap;
} catch (Exception e) {
e.printStackTrace();
// 这里是读取文件产生异常
} finally {
if (fis != null) {
try {
fis.close();
} catch (IOException e) {
// fis流关闭异常
e.printStackTrace();
}
}
if (ois != null) {
try {
ois.close();
} catch (IOException e) {
// ois流关闭异常
e.printStackTrace();
}
}
}
// 读取产生异常,返回null
return null;
}


//通过这种方式保存在本地的图片,是可以看到的

public static void saveBitmap2(Bitmap mBitmap, String imageURL, Context cxt) {


String bitmapName = imageURL.substring(imageURL.lastIndexOf("/") + 1);


FileOutputStream fos = null;


try {
fos = cxt.openFileOutput(bitmapName, Context.MODE_PRIVATE);
mBitmap.compress(Bitmap.CompressFormat.PNG, 100, fos);
fos.flush();
fos.close();
} catch (Exception e) {
e.printStackTrace();
// 这里是保存文件产生异常
} finally {
if (fos != null) {
try {
fos.close();
} catch (IOException e) {
// fos流关闭异常
e.printStackTrace();
}
}
}
}


public static Bitmap getBitmap2(String fileName, Context cxt) {
String bitmapName = fileName.substring(fileName.lastIndexOf("/") + 1);
FileInputStream fis = null;
try {
fis = cxt.openFileInput(bitmapName);
byte[] b = new byte[fis.available()];
fis.read(b);
fis.close();
Bitmap bitmap = BitmapFactory.decodeByteArray(b, 0, b.length);
return bitmap;
} catch (Exception e) {
e.printStackTrace();
// 这里是读取文件产生异常
} finally {
if (fis != null) {
try {
fis.close();
} catch (IOException e) {
// fis流关闭异常
e.printStackTrace();
}
}
}
// 读取产生异常,返回null
return null;
}


/**
* 判断本地的私有文件夹里面是否存在当前名字的文件
*/
public static boolean isFileExist(String fileName, Context cxt) {
String bitmapName = fileName.substring(fileName.lastIndexOf("/") + 1);
List<String> nameLst = Arrays.asList(cxt.fileList());
if (nameLst.contains(bitmapName)) {
return true;
} else {
return false;
}

}


2.在application里配置ImageLoader

/**
* 获得图片加载器

* @return
*/
public ImageLoader getImageLoader() {
if (mImageLoader == null) {

                      // 这个是ImageLoader 的缓存,每次新启动应用,都会走这里
final LruCache<String, Bitmap> mImageCache = new LruCache<String, Bitmap>(
               20);
       ImageCache imageCache = new ImageCache() {
           @Override
           public void putBitmap(String key, Bitmap value) {
               mImageCache.put(key, value);
    // 保存到本地
    ComTools.saveBitmap2(value, key, getApplicationContext());
           }


           @Override
           public Bitmap getBitmap(String key) {
               return mImageCache.get(key);
           }
       };
mImageLoader = new ImageLoader(getRequestQueue(), imageCache);
}


return mImageLoader;
}


3.调用

String imgUrl ="XXXXXXX";// url地址

boolean isFileExist = ComTools.isFileExist(imgUrl, cxt);

if (isFileExist) {
Bitmap bitmap = ComTools.getBitmap2(imgUrl, cxt);
iv.setImageBitmap(bitmap);

}else {
//如果有网络的话,再通过vollery调用
}

0 0