踩坑记录(4)-DiskLruCache需要权限

来源:互联网 发布:narcissu 知乎 编辑:程序博客网 时间:2024/06/07 11:05

Volley的ImageLoader里要自己写个缓存,于是上网看了几篇文章,感觉都差不多,就照抄下来,一次成功(android 4.4),效果良好。
后来换了个android 4.2的机器测试就出错了,在下图位置报了一个空指针(其实写了什么并不重要)

最后在
http://stackoverflow.com/questions/14391314/android-getexternalcachedir-returns-null
发现答案。

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.

感觉好气。。。然后看了下评论

On android 4.4+ this permission is no longer required for cache purpose You can use this permission with the maxSdkVersion paremeter:
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" android:maxSdkVersion="18" />

-

And on some lollipop builds this is broken, so I suggest removing the max sdk … Silly I know

另:
写了一个获取网易云音乐歌曲封面的app,已经能看出形状了,
https://github.com/mmga/CloudCover

0 0
原创粉丝点击