Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android getExternalCacheDir() returns null

I am trying to implement the best practices described in Loading Bitmaps effeciently

I've run into trouble because this line:

Utils.getExternalCacheDir(context)

inside of DiskLruCache.java is returning null, which means I get NullPointerException when I try to call .getPath()

like image 877
FoamyGuy Avatar asked Jan 18 '13 01:01

FoamyGuy


2 Answers

Despite the somewhat cryptic NullPointerException that gets thrown the actual issues is that my application did not have WRITE_EXTERNAL permission, so the system was rejecting my attempt to use the ExternalDir for caching. Unfortunately this was happening at a low enough level in the code used in Displaying Bitmaps Effeciently that the Exception does not indicate SecurityException as it normally would if one were trying to write to the SD card without the proper permission.

To fix simply add this:

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>

to your manifest.

like image 143
FoamyGuy Avatar answered Sep 21 '22 00:09

FoamyGuy


This can also happen if you're running on a device or emulator without any external storage.

like image 31
Anton I. Sipos Avatar answered Sep 19 '22 00:09

Anton I. Sipos