sd卡里的android,关于缓存图片到sd卡里的解决方案

Java codeFile file = new File(Environment.getExternalStorageDirectory()+"/device.png");

InputStream inputStream = new FileInputStream(file);

long length = file.length();

byte[] bytes = new byte[(int)length];

int offset = 0;

int numRead = 0;

while (offset < bytes.length && (numRead= inputStream.read(bytes, offset, bytes.length-offset)) >= 0)

{

offset += numRead;

}

Bitmap bitmap = BitmapFactory.decodeByteArray(bytes, 0, bytes.length);

------解决方案--------------------

思路就是这样的

你需要有个配合记录的文件,比如数据库,我们的项目里是这么干的

下载图片获取Bitmap,将bitmap写到sd卡,同时将图片sd卡保存的路径和图片对应的网络url做为一条记录存起。

读取图片的时候,优先通过数据库获取url对应的本地路径的图片,没在本地取到bitmap,再去网络获取

至于具体实现,楼主还是自己来吧,没什么难度

------解决方案--------------------

我对图片出来的类:

package com.mandao.animal1.util;

import java.io.File;

import java.io.FileNotFoundException;

import java.io.FileOutputStream;

import java.io.IOException;

import java.io.OutputStream;

import java.util.Arrays;

import java.util.Comparator;

import java.util.List;

import android.graphics.Bitmap;

import android.os.Environment;

import android.os.StatFs;

import android.util.Log;

import com.mandao.animal1.common.Config;

/**

* 将图片存到sdcard中。

* @author xiaomiao

*

*/

public class SaveImageToSdcard {

private final String TAG = this.getClass().toString();

private int imageLength;

public final int CACHE_SIZE = 20; //设置保存图片文件夹为20M

public final static int MB = 1024 * 1024;

public final int OVER_TIME = 14 * 24 * 60 * 60 * 1000; //过期时间总数是两个星期

public final String WHOLESALE_CONV = ".png";

/**

* 构造函数传递图片大小

* @param imgLength

*/

public SaveImageToSdcard(int imgLength){

this.imageLength = imgLength;

}

/**

* 图片保存

* @param 图片

* @param 保存路径地址

*/

public void saveBmpToSd(Bitmap bm, String url,int chapter, String bookName) {

if (bm == null) {

Log.w(TAG, " trying to save null bitmap");

return;

}

// 判断sdcard上的空间

if (imageLength > freeSpaceOnSd()) {

Log.w(TAG, "Low free space onsd, do not cache");

return;

}

//截取图片名称的后面名字

String filename = convertUrlToFileName(url);

try {

File file = new File(Config.FILEDIR);

if(!file.exists()){

file.mkdir();

}

//创建一个目录

file = new File(Config.FILEDIR + File.separator + bookName);

if(!file.exists()){

file.mkdir();

}

//当chapter为0时表示图片为封面,保存在漫画目录下

if(chapter == 0){

file = new File(Config.FILEDIR + File.separator + bookName, filename);

}else{

//创建章节目录

file = new File(Config.FILEDIR + File.separator + bookName + File.separator + chapter);

if(!file.exists()){

file.mkdir();

}

//在目录下创建图片文件

file = new File(Config.FILEDIR + File.separator + bookName + File.separator + chapter, filename);

}

先删除两个星期没用过的图片

//removeExpiredCache(FILEDIR + File.separator + bookName);

要是还是不够大,删除近期没用过的图片

//removeCache(FILEDIR + File.separator + bookName);

if (!file.exists()) {

file.createNewFile();

}

OutputStream outStream = new FileOutputStream(file);

bm.compress(Bitmap.CompressFormat.PNG, 100, outStream);

outStream.flush();

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值