Android : Issue while Caching the image in Disk

on Monday, October 6, 2014


Am currently developing a custom gallery where am using DiskLruCache ..Unfortunately, while am running am getting the following error



java.lang.IllegalStateException: edit didn't create file 1


here is my code for caching image to disk



public void addBitmapToCache(String data, BitmapDrawable value) {
//BEGIN_INCLUDE(add_bitmap_to_cache)
if (data == null || value == null) {
return;
}

// Add to memory cache
if (mMemCache != null) {
if (RecyclingBitmapDrawable.class.isInstance(value)) {
// The removed entry is a recycling drawable, so notify it
// that it has been added into the memory cache
((RecyclingBitmapDrawable) value).setIsCached(true);
}
mMemCache.put(data, value);
}

synchronized (mDiskCacheLock) {
// Add to disk cache
if (mDiskLruCache != null) {
final String key = DiskLruCache.hashKeyForDisk(data);
OutputStream out = null;
try {
DiskLruCache.Snapshot snapshot = mDiskLruCache.get(key);
if (snapshot == null) {
final DiskLruCache.Editor editor = mDiskLruCache.edit(key);
if (editor != null) {
out = editor.newOutputStream(0);
value.getBitmap().compress(CompressFormat.JPEG, 100, out);
editor.commit();
out.close();
}
} else {
snapshot.getInputStream(0).close();
}
} catch (final IOException e) {
Log.e("disk", "addBitmapToCache - " + e);
} catch (Exception e) {
Log.e("disk", "addBitmapToCache - " + e);
} finally {
try {
if (out != null) {
out.close();
}
} catch (IOException e) {}
}
}
}

}


why am getting this error and what is the solution for this?


0 comments:

Post a Comment