Skip to content
This repository was archived by the owner on Oct 15, 2018. It is now read-only.

Commit e38f273

Browse files
author
Chris Banes
committed
Merge branch 'dev'
2 parents 98eac0a + b75c98c commit e38f273

File tree

12 files changed

+98
-55
lines changed

12 files changed

+98
-55
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ it distributed to the central repositories. Simply add the following to your
4242
<dependency>
4343
<groupId>com.github.chrisbanes.bitmapcache</groupId>
4444
<artifactId>library</artifactId>
45-
<version>2.2.1</version>
45+
<version>2.2.2</version>
4646
</dependency>
4747
```
4848

library/AndroidManifest.xml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
<?xml version="1.0" encoding="utf-8"?>
22
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
33
package="uk.co.senab.bitmapcache"
4-
android:versionCode="2210"
5-
android:versionName="2.2.1" >
4+
android:versionCode="2220"
5+
android:versionName="2.2.2" >
66

77
<uses-sdk android:minSdkVersion="4" />
88

-22 KB
Binary file not shown.
21.2 KB
Binary file not shown.

library/pom.xml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
<parent>
1111
<groupId>com.github.chrisbanes.bitmapcache</groupId>
1212
<artifactId>parent</artifactId>
13-
<version>2.2.1</version>
13+
<version>2.2.3-SNAPSHOT</version>
1414
</parent>
1515

1616
<dependencies>
@@ -22,7 +22,7 @@
2222
<groupId>com.jakewharton</groupId>
2323
<artifactId>disklrucache</artifactId>
2424
<type>jar</type>
25-
<version>1.3.1</version>
25+
<version>2.0.1</version>
2626
</dependency>
2727
<dependency>
2828
<groupId>com.google.android</groupId>

library/src/uk/co/senab/bitmapcache/BitmapLruCache.java

Lines changed: 52 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616

1717
package uk.co.senab.bitmapcache;
1818

19-
import com.jakewharton.DiskLruCache;
19+
import com.jakewharton.disklrucache.DiskLruCache;
2020

2121
import android.content.Context;
2222
import android.content.res.Resources;
@@ -31,6 +31,7 @@
3131
import java.io.File;
3232
import java.io.IOException;
3333
import java.io.InputStream;
34+
import java.io.OutputStream;
3435
import java.util.HashMap;
3536
import java.util.concurrent.ScheduledFuture;
3637
import java.util.concurrent.ScheduledThreadPoolExecutor;
@@ -334,6 +335,25 @@ public boolean isMemoryCacheEnabled() {
334335
* @return CacheableBitmapDrawable which can be used to display the bitmap.
335336
*/
336337
public CacheableBitmapDrawable put(final String url, final Bitmap bitmap) {
338+
return put(url, bitmap, Bitmap.CompressFormat.PNG, 100);
339+
}
340+
341+
/**
342+
* Caches {@code bitmap} for {@code url} into all enabled caches. If the disk cache is enabled,
343+
* the bitmap will be compressed with the settings you provide.
344+
* <p/> If you have the disk cache enabled, you should not call this method from main/UI thread.
345+
*
346+
* @param url - String representing the URL of the image.
347+
* @param bitmap - Bitmap which has been decoded from {@code url}.
348+
* @param compressFormat - Compression Format to use
349+
* @param compressQuality - Level of compression to use
350+
* @return CacheableBitmapDrawable which can be used to display the bitmap.
351+
*
352+
* @see Bitmap#compress(Bitmap.CompressFormat, int, OutputStream)
353+
*/
354+
public CacheableBitmapDrawable put(final String url, final Bitmap bitmap,
355+
Bitmap.CompressFormat compressFormat, int compressQuality) {
356+
337357
CacheableBitmapDrawable d = new CacheableBitmapDrawable(url, mResources, bitmap,
338358
mRecyclePolicy);
339359

@@ -347,13 +367,26 @@ public CacheableBitmapDrawable put(final String url, final Bitmap bitmap) {
347367
final String key = transformUrlForDiskCacheKey(url);
348368
final ReentrantLock lock = getLockForDiskCacheEdit(key);
349369
lock.lock();
370+
371+
OutputStream os = null;
372+
350373
try {
351374
DiskLruCache.Editor editor = mDiskCache.edit(key);
352-
Util.saveBitmap(bitmap, editor.newOutputStream(0));
375+
os = editor.newOutputStream(0);
376+
bitmap.compress(compressFormat, compressQuality, os);
377+
os.flush();
353378
editor.commit();
354379
} catch (IOException e) {
355-
e.printStackTrace();
380+
Log.e(Constants.LOG_TAG, "Error while writing to disk cache", e);
356381
} finally {
382+
if (null != os) {
383+
try {
384+
os.close();
385+
} catch (IOException e) {
386+
Log.e(Constants.LOG_TAG, "Failed to close output stream", e);
387+
}
388+
}
389+
357390
lock.unlock();
358391
scheduleDiskCacheFlush();
359392
}
@@ -406,16 +439,9 @@ public CacheableBitmapDrawable put(final String url, final InputStream inputStre
406439
tmpFile = File.createTempFile("bitmapcache_", null, mTempDir);
407440

408441
// Pipe InputStream to file
409-
Util.copy(inputStream, tmpFile);
410-
411-
try {
412-
// Close the original InputStream
413-
inputStream.close();
414-
} catch (IOException e) {
415-
// NO-OP - Ignore
416-
}
442+
IoUtils.copy(inputStream, tmpFile);
417443
} catch (IOException e) {
418-
e.printStackTrace();
444+
Log.e(Constants.LOG_TAG, "Error writing to saving stream to temp file: " + url, e);
419445
}
420446

421447
CacheableBitmapDrawable d = null;
@@ -433,15 +459,16 @@ public CacheableBitmapDrawable put(final String url, final InputStream inputStre
433459
}
434460

435461
if (null != mDiskCache) {
462+
final String key = transformUrlForDiskCacheKey(url);
436463
final ReentrantLock lock = getLockForDiskCacheEdit(url);
437464
lock.lock();
465+
438466
try {
439-
DiskLruCache.Editor editor = mDiskCache
440-
.edit(transformUrlForDiskCacheKey(url));
441-
Util.copy(tmpFile, editor.newOutputStream(0));
467+
DiskLruCache.Editor editor = mDiskCache.edit(key);
468+
IoUtils.copy(tmpFile, editor.newOutputStream(0));
442469
editor.commit();
443470
} catch (IOException e) {
444-
e.printStackTrace();
471+
Log.e(Constants.LOG_TAG, "Error writing to disk cache. URL: " + url, e);
445472
} finally {
446473
lock.unlock();
447474
scheduleDiskCacheFlush();
@@ -728,18 +755,21 @@ public Builder setRecyclePolicy(RecyclePolicy recyclePolicy) {
728755
}
729756

730757
private boolean isValidOptionsForDiskCache() {
731-
if (mDiskCacheEnabled) {
758+
boolean valid = mDiskCacheEnabled;
759+
760+
if (valid) {
732761
if (null == mDiskCacheLocation) {
733762
Log.i(Constants.LOG_TAG,
734763
"Disk Cache has been enabled, but no location given. Please call setDiskCacheLocation(...)");
735-
return false;
764+
valid = false;
736765
} else if (!mDiskCacheLocation.canWrite()) {
737-
throw new IllegalArgumentException("Disk Cache Location is not write-able");
766+
Log.i(Constants.LOG_TAG,
767+
"Disk Cache Location is not write-able, disabling disk caching.");
768+
valid = false;
738769
}
739-
740-
return true;
741770
}
742-
return false;
771+
772+
return valid;
743773
}
744774

745775
private boolean isValidOptionsForMemoryCache() {

library/src/uk/co/senab/bitmapcache/CacheableBitmapDrawable.java

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -51,10 +51,13 @@ public class CacheableBitmapDrawable extends BitmapDrawable {
5151
// Handler which may be used later
5252
private static final Handler sHandler = new Handler(Looper.getMainLooper());
5353

54+
private final int mMemorySize;
55+
5456
CacheableBitmapDrawable(String url, Resources resources, Bitmap bitmap,
5557
BitmapLruCache.RecyclePolicy recyclePolicy) {
5658
super(resources, bitmap);
5759

60+
mMemorySize = null != bitmap ? (bitmap.getRowBytes() * bitmap.getHeight()) : 0;
5861
mUrl = url;
5962
mRecyclePolicy = recyclePolicy;
6063
mDisplayingCount = 0;
@@ -78,17 +81,10 @@ public void draw(Canvas canvas) {
7881
}
7982

8083
/**
81-
* @return Amount of heap size currently being used by {@code Bitmap}
84+
* @return Amount of memory currently being used by {@code Bitmap}
8285
*/
8386
int getMemorySize() {
84-
int size = 0;
85-
86-
final Bitmap bitmap = getBitmap();
87-
if (null != bitmap && !bitmap.isRecycled()) {
88-
size = bitmap.getRowBytes() * bitmap.getHeight();
89-
}
90-
91-
return size;
87+
return mMemorySize;
9288
}
9389

9490
/**

library/src/uk/co/senab/bitmapcache/CacheableImageView.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,10 @@ public CacheableImageView(Context context, AttributeSet attrs) {
4343
super(context, attrs);
4444
}
4545

46+
public CacheableImageView(Context context, AttributeSet attrs, int defStyle) {
47+
super(context, attrs, defStyle);
48+
}
49+
4650
@Override
4751
public void setImageDrawable(Drawable drawable) {
4852
final Drawable previousDrawable = getDrawable();

library/src/uk/co/senab/bitmapcache/Util.java renamed to library/src/uk/co/senab/bitmapcache/IoUtils.java

Lines changed: 23 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,7 @@
1515
******************************************************************************/
1616
package uk.co.senab.bitmapcache;
1717

18-
import android.graphics.Bitmap;
19-
import android.graphics.Bitmap.CompressFormat;
18+
import android.util.Log;
2019

2120
import java.io.File;
2221
import java.io.FileInputStream;
@@ -25,7 +24,7 @@
2524
import java.io.InputStream;
2625
import java.io.OutputStream;
2726

28-
class Util {
27+
class IoUtils {
2928

3029
static long copy(File in, OutputStream out) throws IOException {
3130
return copy(new FileInputStream(in), out);
@@ -35,22 +34,32 @@ static long copy(InputStream in, File out) throws IOException {
3534
return copy(in, new FileOutputStream(out));
3635
}
3736

38-
static void saveBitmap(Bitmap bitmap, OutputStream out) {
39-
bitmap.compress(CompressFormat.PNG, 100, out);
40-
}
41-
4237
/**
4338
* Pipe an InputStream to the given OutputStream <p /> Taken from Apache Commons IOUtils.
4439
*/
4540
private static long copy(InputStream input, OutputStream output) throws IOException {
46-
byte[] buffer = new byte[1024 * 4];
47-
long count = 0;
48-
int n;
49-
while (-1 != (n = input.read(buffer))) {
50-
output.write(buffer, 0, n);
51-
count += n;
41+
try {
42+
byte[] buffer = new byte[1024 * 4];
43+
long count = 0;
44+
int n;
45+
while (-1 != (n = input.read(buffer))) {
46+
output.write(buffer, 0, n);
47+
count += n;
48+
}
49+
output.flush();
50+
return count;
51+
} finally {
52+
try {
53+
input.close();
54+
} catch (IOException e) {
55+
Log.i(Constants.LOG_TAG, "Failed to close InputStream", e);
56+
}
57+
try {
58+
output.close();
59+
} catch (IOException e) {
60+
Log.i(Constants.LOG_TAG, "Failed to close OutputStream", e);
61+
}
5262
}
53-
return count;
5463
}
5564

5665
}

pom.xml

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
<groupId>com.github.chrisbanes.bitmapcache</groupId>
66
<artifactId>parent</artifactId>
77
<packaging>pom</packaging>
8-
<version>2.2.1</version>
8+
<version>2.2.3-SNAPSHOT</version>
99
<name>Android-BitmapCache Project</name>
1010
<description>A custom Bitmap cache implementation for Android</description>
1111
<url>https://github.com/chrisbanes/Android-BitmapCache</url>
@@ -28,7 +28,7 @@
2828
<url>https://github.com/chrisbanes/Android-BitmapCache</url>
2929
<connection>scm:git:git://github.com/chrisbanes/Android-BitmapCache.git</connection>
3030
<developerConnection>scm:git:git@github.com:chrisbanes/Android-BitmapCache.git</developerConnection>
31-
<tag>v2.2.1</tag>
31+
<tag>HEAD</tag>
3232
</scm>
3333

3434
<developers>
@@ -73,6 +73,10 @@
7373
<groupId>org.apache.maven.plugins</groupId>
7474
<artifactId>maven-compiler-plugin</artifactId>
7575
<version>2.3.2</version>
76+
<configuration>
77+
<source>${java.version}</source>
78+
<target>${java.version}</target>
79+
</configuration>
7680
</plugin>
7781
<plugin>
7882
<groupId>org.apache.maven.plugins</groupId>

0 commit comments

Comments
 (0)