Android 将磁盘缓存与Volley和ListView一起使用速度非常慢
我正在尝试将此库用于磁盘缓存,但当我的ListView中有很多图像时,我遇到了问题。这种滞后有时真的很严重 我使用了以下类似于GitHub回购的方法: 问题是,对于较大的图像,存在很大的延迟,并且没有良好的滚动体验 如何改进此方法 我可以使用Android 将磁盘缓存与Volley和ListView一起使用速度非常慢,android,listview,bitmap,android-volley,android-lru-cache,Android,Listview,Bitmap,Android Volley,Android Lru Cache,我正在尝试将此库用于磁盘缓存,但当我的ListView中有很多图像时,我遇到了问题。这种滞后有时真的很严重 我使用了以下类似于GitHub回购的方法: 问题是,对于较大的图像,存在很大的延迟,并且没有良好的滚动体验 如何改进此方法 我可以使用public-Bitmap-put-Bitmap(String-key)在后台线程中保存位图,但是public-Bitmap-getBitmap(String-key)在后台线程中无法执行 谢谢。嗨!你解决问题了吗?因为我和你的处境一样!谢谢:) publi
public-Bitmap-put-Bitmap(String-key)
在后台线程中保存位图,但是public-Bitmap-getBitmap(String-key)
在后台线程中无法执行
谢谢。嗨!你解决问题了吗?因为我和你的处境一样!谢谢:)
public DiskLruImageCache(Context context,String uniqueName, int diskCacheSize,
CompressFormat compressFormat, int quality ) {
try {
final File diskCacheDir = getDiskCacheDir(context, uniqueName );
mDiskCache = DiskLruCache.open( diskCacheDir, APP_VERSION, VALUE_COUNT, diskCacheSize );
mCompressFormat = compressFormat;
mCompressQuality = quality;
} catch (IOException e) {
e.printStackTrace();
}
}
private boolean writeBitmapToFile(Bitmap bitmap, DiskLruCache.Editor editor )
throws IOException, FileNotFoundException {
OutputStream out = null;
try {
out = new BufferedOutputStream( editor.newOutputStream( 0 ), IO_BUFFER_SIZE );
return bitmap.compress( mCompressFormat, mCompressQuality, out );
} finally {
if ( out != null ) {
out.close();
}
}
}
private File getDiskCacheDir(Context context, String uniqueName) {
final String cachePath = context.getCacheDir().getPath();
return new File(cachePath + File.separator + uniqueName);
}
@Override
public void putBitmap( String key, Bitmap data ) {
DiskLruCache.Editor editor = null;
try {
editor = mDiskCache.edit( key );
if ( editor == null ) {
return;
}
if( writeBitmapToFile( data, editor ) ) {
mDiskCache.flush();
editor.commit();
if ( BuildConfig.DEBUG ) {
Log.d( "cache_test_DISK_", "image put on disk cache " + key );
}
} else {
editor.abort();
if ( BuildConfig.DEBUG ) {
Log.d( "cache_test_DISK_", "ERROR on: image put on disk cache " + key );
}
}
} catch (IOException e) {
if ( BuildConfig.DEBUG ) {
Log.d( "cache_test_DISK_", "ERROR on: image put on disk cache " + key );
}
try {
if ( editor != null ) {
editor.abort();
}
} catch (IOException ignored) {
}
}
}
@Override
public Bitmap getBitmap( String key ) {
Bitmap bitmap = null;
DiskLruCache.Snapshot snapshot = null;
try {
snapshot = mDiskCache.get( key );
if ( snapshot == null ) {
return null;
}
final InputStream in = snapshot.getInputStream( 0 );
if ( in != null ) {
final BufferedInputStream buffIn =
new BufferedInputStream( in, IO_BUFFER_SIZE );
bitmap = BitmapFactory.decodeStream( buffIn );
}
} catch ( IOException e ) {
e.printStackTrace();
} finally {
if ( snapshot != null ) {
snapshot.close();
}
}
if ( BuildConfig.DEBUG ) {
Log.d( "cache_test_DISK_", bitmap == null ? "" : "image read from disk " + key);
}
return bitmap;
}