このhttp://developer.android.com/training/displaying-bitmaps/cache-bitmap.htmlを読みましたが、ディスクキャッシュをアプリに実装する簡単な方法を探しています。lib http://square.github.io/picasso/が見つかりましたが、機能していません。私はいつも「NoClassDefFoundError」を受け取ります。ダウンロードしたビットマップを簡単にキャッシュできるライブラリを知っていますか? ありがとうございました
4438 次
4 に答える
0
「android Stdioのソリューション」
picasso lib を使用するには、build.gradle の
依存関係セクションにエントリを追加する必要があります。
「com.squareup.picasso:picasso:2.5.2」をコンパイルします
「NoClassDefFoundError」は発生せず、ピカソを使用して画像をキャッシュできるようになりました
于 2015-08-13T17:01:14.983 に答える
0
このコードを試して、url から画像をキャッシュできます。
import java.util.Collections;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.Map.Entry;
import android.graphics.Bitmap;
public class MemoryCache {
String TAG = "MemoryCache";
private Map<String, Bitmap> cache=Collections.synchronizedMap(
new LinkedHashMap<String, Bitmap>(10,1.5f,true));//Last argument true for LRU ordering
private long size=0;//current allocated size
private long limit=1000000;//max memory in bytes
public MemoryCache(){
//use 25% of available heap size
setLimit(Runtime.getRuntime().maxMemory()/4);
}
public void setLimit(long new_limit){
limit=new_limit;
}
public Bitmap get(String id){
try{
if(!cache.containsKey(id))
return null;
//NullPointerException sometimes happen here http://code.google.com/p/osmdroid/issues/detail?id=78
return cache.get(id);
}catch(NullPointerException ex){
return null;
}
}
public void put(String id, Bitmap bitmap){
try{
if(cache.containsKey(id))
size-=getSizeInBytes(cache.get(id));
cache.put(id, bitmap);
size+=getSizeInBytes(bitmap);
checkSize();
}catch(Throwable th){
th.printStackTrace();
}
}
private void checkSize() {
if(size>limit){
Iterator<Entry<String, Bitmap>> iter=cache.entrySet().iterator();//least recently accessed item will be the first one iterated
while(iter.hasNext()){
Entry<String, Bitmap> entry=iter.next();
size-=getSizeInBytes(entry.getValue());
iter.remove();
if(size<=limit)
break;
}
}
}
public void clear() {
cache.clear();
}
long getSizeInBytes(Bitmap bitmap) {
if(bitmap==null)
return 0;
return bitmap.getRowBytes() * bitmap.getHeight();
}
}
使用法は:
memoryCache.put(photoToLoad.url, bmp);
そして取得する:
memoryCache.get(url);
質問がある場合は、コメントでお気軽に質問してください!
于 2013-07-04T05:29:45.070 に答える