我尝试将图像加载到轮播视图中。这些图像是从互联网上下载的。我们使用三个图像 url 来下载图像。我想将这三个图像保存在缓存中的三个单独文件夹中。我做了以下方式。但是所有三张图片都保存在缓存中的一个文件夹中。所以我无法在保存缓存后单独访问这三张图片。
这是我的 ImageLoader 缓存......
public class ImageLoader {
private static final String TAG = "ImageLoader";
ProgressBar progressbar;
LinearLayout layout;
int widthScreen,heightScreen;
MemoryCache memoryCache=new MemoryCache();
FileCache fileCache;
private Map<ImageView, String> imageViews=Collections.synchronizedMap(new WeakHashMap<ImageView, String>());
ExecutorService executorService;
Handler handler=new Handler();//handler to display images in UI thread
int sample;
Context context;
public ImageLoader(Context context){
this.context=context;
fileCache=new FileCache(context);
executorService=Executors.newFixedThreadPool(5);
}
public ImageLoader(Context context,int i){
this.context=context;
//fileCache=new FileCache(context);
//executorService=Executors.newFixedThreadPool(5);
}
final int stub_id=R.drawable.tnlrocks;
public void DisplayImage(String url, ImageView imageView)
{
imageViews.put(imageView, url);
Bitmap bitmapMemory=memoryCache.get(url);
queuePhoto(url, imageView);
}
private void queuePhoto(String url, ImageView imageView)
{
PhotoToLoad p=new PhotoToLoad(url, imageView);
executorService.submit(new PhotosLoader(p));
}
private Bitmap getBitmap(String url) throws FileNotFoundException
{
File f=fileCache.getFile(url);
//from SD cache
Bitmap b = decodeFile(f);
if(b!=null)
return b;
//from web
try {
Bitmap bitmap=null;
URL imageUrl = new URL(url);
HttpURLConnection conn = (HttpURLConnection)imageUrl.openConnection();
conn.setConnectTimeout(300000000);
conn.setReadTimeout(300000000);
conn.setInstanceFollowRedirects(true);
InputStream inputstream=conn.getInputStream();
OutputStream outputstream = new FileOutputStream(f);
Utils.CopyStream(inputstream, outputstream);
outputstream.close();
conn.disconnect();
bitmap = decodeFile(f);
return bitmap;
} catch (Throwable ex){
ex.printStackTrace();
if(ex instanceof OutOfMemoryError)
memoryCache.clear();
return null;
}
}
public void getDimension(int width,int height){
widthScreen=width;
heightScreen=height;
}
//decodes image and scales it to reduce memory consumption
private Bitmap decodeFile(File f){
try {
//decode image size
final int IMAGE_MAX_SIZE = 120000000; // 1.2MP
BitmapFactory.Options scaleOptions = new BitmapFactory.Options();
scaleOptions.inJustDecodeBounds = true;
FileInputStream stream1=new FileInputStream(f);
BitmapFactory.decodeStream(stream1,null,scaleOptions);
stream1.close();
// find the correct scale value as a power of 2.
int scale = 1;
while (scaleOptions.outWidth / scale / 2 >= widthScreen
&& scaleOptions.outHeight / scale / 2 >= heightScreen) {
scale *= 2;
}
Bitmap bitmap = null;
if (scale > 1) {
scale--;
// scale to max possible inSampleSize that still yields an image
// larger than target
scaleOptions = new BitmapFactory.Options();
scaleOptions.inSampleSize = scale;
bitmap = BitmapFactory.decodeStream(stream1, null, scaleOptions);
int width=widthScreen;
int height=heightScreen;
double y=height;
double x=width;
Bitmap scaledBitmap = Bitmap.createScaledBitmap(bitmap, (int) x,
(int) y, true);
bitmap.recycle();
bitmap = scaledBitmap;
System.gc();
} else {
bitmap = BitmapFactory.decodeStream(stream1);
}
if((widthScreen>=450)&& (heightScreen>=750) ) {
sample=1;
}
else{
sample=2;
}
//decode with inSampleSize
BitmapFactory.Options scalOption = new BitmapFactory.Options();
// scalOption.inSampleSize=sample;
FileInputStream stream2=new FileInputStream(f);
Bitmap bitMap=BitmapFactory.decodeStream(stream2, null, scalOption);
Bitmap resizedbitmap=Bitmap.createScaledBitmap(bitMap, widthScreen/2, heightScreen/3, true);
stream2.close();
return resizedbitmap;
} catch (FileNotFoundException e) {
}
catch (IOException e) {
e.printStackTrace();
}
return null;
}
//Task for the queue
private class PhotoToLoad
{
public String url;
public ImageView imageView;
public PhotoToLoad(String u, ImageView i){
url=u;
imageView=i;
}
}
class PhotosLoader implements Runnable {
PhotoToLoad photoToLoad;
PhotosLoader(PhotoToLoad photoToLoad){
this.photoToLoad=photoToLoad;
}
@Override
public void run() {
try{
if(imageViewReused(photoToLoad))
return;
String s=photoToLoad.url;
if(s!=null){
}
Bitmap bmp=getBitmap(photoToLoad.url);
memoryCache.put(photoToLoad.url, bmp);
if(imageViewReused(photoToLoad))
return;
BitmapDisplayer bd=new BitmapDisplayer(bmp, photoToLoad);
handler.post(bd);
}catch(Throwable th){
th.printStackTrace();
}
}
}
boolean imageViewReused(PhotoToLoad photoToLoad){
String tag=imageViews.get(photoToLoad.imageView);
if(tag==null || !tag.equals(photoToLoad.url))
return true;
return false;
}
//Used to display bitmap in the UI thread
class BitmapDisplayer implements Runnable
{
//Bitmap icon=BitmapFactory.decodeResource(context.getResources(), R.drawable.tnlrocks);
//Bitmap resizedbitmap=Bitmap.createScaledBitmap(icon, widthScreen, heightScreen, true);
Bitmap bitmap;
PhotoToLoad photoToLoad;
public BitmapDisplayer(Bitmap b, PhotoToLoad p){bitmap=b;photoToLoad=p;}
public void run()
{
if(imageViewReused(photoToLoad))
return;
if(bitmap!=null)
photoToLoad.imageView.setImageBitmap(bitmap);
else
//photoToLoad.imageView.setImageBitmap(icon);
photoToLoad.imageView.setImageResource(stub_id);
}
}
public void clearCache() {
memoryCache.clear();
//fileCache.clear();
}
}
这是我的 FileCache 类。这里我给目录....
public class FileCache {
private File cacheDir;
public FileCache(Context context){
//Find the dir to save cached images
if (android.os.Environment.getExternalStorageState().equals(android.os.Environment.MEDIA_MOUNTED))
cacheDir=new File(android.os.Environment.getExternalStorageDirectory(),"TNLRadio/res/events");
else
cacheDir=context.getCacheDir();
if(!cacheDir.exists())
cacheDir.mkdirs();
}
public File getFile(String url){
//I identify images by hashcode. Not a perfect solution, good for the demo.
String filename=String.valueOf(url.hashCode());
//Another possible solution (thanks to grantland)
//String filename = URLEncoder.encode(url);
File f = new File(cacheDir, filename);
return f;
}
public void clear(){
File[] files=cacheDir.listFiles();
if(files==null)
return;
for(File f:files)
f.delete();
}
}
那么如何将图像分别保存在缓存中的三个目录并访问它们......请帮助我......