If you think the Android project droid-fu listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.
Java Source Code
/* Copyright (c) 2009 Matthias Kaeppler
*//fromwww.java2s.com
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/package com.github.droidfu.cachefu;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
/**
* Implements a cache capable of caching image files. It exposes helper methods to immediately
* access binary image data as {@link Bitmap} objects.
*
* @author Matthias Kaeppler
*
*/publicclass ImageCache extends AbstractCache<String, byte[]> {
public ImageCache(int initialCapacity, long expirationInMinutes, int maxConcurrentThreads) {
super("ImageCache", initialCapacity, expirationInMinutes, maxConcurrentThreads);
}
publicsynchronizedvoid removeAllWithPrefix(String urlPrefix) {
CacheHelper.removeAllWithStringPrefix(this, urlPrefix);
}
@Override
public String getFileNameForKey(String imageUrl) {
return CacheHelper.getFileNameFromUrl(imageUrl);
}
@Override
protectedbyte[] readValueFromDisk(File file) throws IOException {
BufferedInputStream istream = new BufferedInputStream(new FileInputStream(file));
long fileSize = file.length();
if (fileSize > Integer.MAX_VALUE) {
thrownew IOException("Cannot read files larger than " + Integer.MAX_VALUE + " bytes");
}
int imageDataLength = (int) fileSize;
byte[] imageData = newbyte[imageDataLength];
istream.read(imageData, 0, imageDataLength);
istream.close();
return imageData;
}
publicsynchronized Bitmap getBitmap(Object elementKey) {
byte[] imageData = super.get(elementKey);
if (imageData == null) {
return null;
}
return BitmapFactory.decodeByteArray(imageData, 0, imageData.length);
}
@Override
protectedvoid writeValueToDisk(File file, byte[] imageData) throws IOException {
BufferedOutputStream ostream = new BufferedOutputStream(new FileOutputStream(file));
ostream.write(imageData);
ostream.close();
}
}