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

本文分享了在使用Volley的ImageLoader时遇到的缓存问题及解决方案。问题出现在Android 4.2设备上,表现为在尝试使用外部缓存目录时抛出空指针异常。文章详细介绍了如何通过添加WRITE_EXTERNAL_STORAGE权限来修复该问题,并提供了适用于不同Android版本的配置建议。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值