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

Commit

Permalink
[library] Fix saving files when on a device without external storage.
Browse files Browse the repository at this point in the history
The reason was that createTempFile defaults to the
 external storage. I've now added a new Builder constructor
 which takes a Context. We now use getCacheDir as the
 dir for temp files.
  • Loading branch information
Chris Banes committed Feb 23, 2013
1 parent 39b6226 commit cd40907
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 12 deletions.
47 changes: 36 additions & 11 deletions library/src/uk/co/senab/bitmapcache/BitmapLruCache.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@

import com.jakewharton.DiskLruCache;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.AsyncTask;
Expand Down Expand Up @@ -121,11 +122,19 @@ private static String transformUrlForDiskCacheKey(String url) {
return Md5.encode(url);
}

private DiskLruCache mDiskCache;
private File mTempDir;

private final BitmapMemoryLruCache mMemoryCache;
/**
* Memory Cache Variables
*/
private BitmapMemoryLruCache mMemoryCache;

private final RecyclePolicy mRecyclePolicy;
private RecyclePolicy mRecyclePolicy;

/**
* Disk Cache Variables
*/
private DiskLruCache mDiskCache;

// Variables which are only used when the Disk Cache is enabled
private HashMap<String, ReentrantLock> mDiskCacheEditLocks;
Expand All @@ -137,9 +146,10 @@ private static String transformUrlForDiskCacheKey(String url) {
// Transient
private ScheduledFuture<?> mDiskCacheFuture;

protected BitmapLruCache(BitmapMemoryLruCache memoryCache, RecyclePolicy recyclePolicy) {
mMemoryCache = memoryCache;
mRecyclePolicy = recyclePolicy;
BitmapLruCache(Context context) {
if (null != context) {
mTempDir = context.getCacheDir();
}
}

/**
Expand Down Expand Up @@ -369,7 +379,7 @@ public CacheableBitmapDrawable put(final String url, final InputStream inputStre
// can be read multiple times
File tmpFile = null;
try {
tmpFile = File.createTempFile("bitmapcache_", null);
tmpFile = File.createTempFile("bitmapcache_", null, mTempDir);

// Pipe InputStream to file
Util.copy(inputStream, tmpFile);
Expand Down Expand Up @@ -464,6 +474,11 @@ synchronized void setDiskCache(DiskLruCache diskCache) {
}
}

void setMemoryCache(BitmapMemoryLruCache memoryCache, RecyclePolicy recyclePolicy) {
mMemoryCache = memoryCache;
mRecyclePolicy = recyclePolicy;
}

private ReentrantLock getLockForDiskCacheEdit(String url) {
synchronized (mDiskCacheEditLocks) {
ReentrantLock lock = mDiskCacheEditLocks.get(url);
Expand Down Expand Up @@ -524,6 +539,8 @@ private static long getHeapSize() {
return Runtime.getRuntime().maxMemory();
}

private Context mContext;

private boolean mDiskCacheEnabled;

private File mDiskCacheLocation;
Expand All @@ -536,7 +553,17 @@ private static long getHeapSize() {

private RecyclePolicy mRecyclePolicy;

/**
* @deprecated You should now use {@link Builder(Context)}. This is so that we can reliably
* find a write-able location for temporary files.
*/
public Builder() {
this(null);
}

public Builder(Context context) {
mContext = context;

// Disk Cache is disabled by default, but it's default size is set
mDiskCacheMaxSize = DEFAULT_DISK_CACHE_MAX_SIZE_MB * MEGABYTE;

Expand All @@ -551,17 +578,15 @@ public Builder() {
* builder.
*/
public BitmapLruCache build() {
BitmapMemoryLruCache memoryCache = null;
final BitmapLruCache cache = new BitmapLruCache(mContext);

if (isValidOptionsForMemoryCache()) {
if (Constants.DEBUG) {
Log.d("BitmapLruCache.Builder", "Creating Memory Cache");
}
memoryCache = new BitmapMemoryLruCache(mMemoryCacheMaxSize);
cache.setMemoryCache(new BitmapMemoryLruCache(mMemoryCacheMaxSize), mRecyclePolicy);
}

final BitmapLruCache cache = new BitmapLruCache(memoryCache, mRecyclePolicy);

if (isValidOptionsForDiskCache()) {
new AsyncTask<Void, Void, DiskLruCache>() {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
* See the License for the specific language governing permissions and
* limitations under the License.
******************************************************************************/

package uk.co.senab.bitmapcache.samples;

import android.app.Application;
Expand Down Expand Up @@ -43,7 +44,7 @@ public void onCreate() {
}
cacheLocation.mkdirs();

BitmapLruCache.Builder builder = new BitmapLruCache.Builder();
BitmapLruCache.Builder builder = new BitmapLruCache.Builder(this);
builder.setMemoryCacheEnabled(true).setMemoryCacheMaxSizeUsingHeapSize();
builder.setDiskCacheEnabled(true).setDiskCacheLocation(cacheLocation);

Expand Down

0 comments on commit cd40907

Please sign in to comment.