这是我创建把位图入高速缓存的方法..如何缩放位图放置,并最初从缓存加载?
//Caching method to cache images loaded from the URL's.
public void putBitmapInDiskCache(URI imageUri, Bitmap avatar) {
File cacheDir = new File(MainMenu.this.getCacheDir(), "thumbnails");
cacheDir.mkdirs();
File cacheFile = new File(cacheDir, ""+imageUri.hashCode());
try {
cacheFile.createNewFile();
FileOutputStream fos = new FileOutputStream(cacheFile);
avatar.compress(CompressFormat.PNG, 100, fos);
fos.flush();
fos.close();
} catch (Exception e) {
Log.e("error", "Error when saving image to cache. ", e);
}
}
唯一的问题是我最初设定的位图的宽度和高度参数时,它被下载。但是当它被放入缓存并被拉出时,它就会失去宽度和高度,并变得很大。当它从屏幕上滚动时,它会再次正确设置。内在地失去了它的宽度和高度。
编辑:我用我的getimagefromCache()方法加载位图。
public void getImagesfromCache() throws MalformedURLException{
if(new File(new File(this.getApplicationContext().getCacheDir(), "thumbnails"),"" + imageCacheUri.hashCode()).exists()){
String cacheFile = this.getApplicationContext().getCacheDir() +"/thumbnails/"+ imageCacheUri.hashCode();
ImageView i = new ImageView(this.getApplicationContext());
FileInputStream fis = null;
try {
fis = new FileInputStream(cacheFile);
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
Bitmap bm = BitmapFactory.decodeStream(fis);
i.setImageBitmap(bm);
i.setTag(mImageURLs);
putBitmapInDiskCache(imageCacheUri, bm);
//Set the BaseAdapter to a gallery, holding the images
((Gallery) findViewById(R.id.gallery))
.setAdapter(new ImageAdapter(MainMenu.this));
如何加载位图图像。看看代码会更好 – Ronnie
看看我的编辑 – yoshi24
你为什么叫putBitmapInDiskCache得到? – Ronnie