android webview img标签加载图片问题 1C

请教一个问题,我在webview中的img标签中显示图片,显示getCacheDir( )目录下的图片显示不出来,但显示getExternalCacheDir()中的图片就行,路径是都是正确的,这是什么原因?

2个回答

你的缓存文件存储在SDCard上,所以要用getExternalCacheDir

u012581732
周小宇 我是新手具体一点,iss样式文件路径不解析
大约 4 年之前 回复
a874508605
问剑孤鸣 我响应webview的点击,调用系统的拍照,拍照后存储在getExternalCacheDir()目录下,回传给webview中的img标签能显示,但是存储在getCacheDir( )目录下的图片就不能显示了
大约 4 年之前 回复

getExternalCacheDir() 方法 API 如下:
File android.content.ContextWrapper.getExternalCacheDir()

Returns the absolute path to the directory on the primary external filesystem (that is somewhere on Environment.getExternalStorageDirectory() where the application can place cache files it owns. These files are internal to the application, and not typically visible to the user as media.

This is like getCacheDir() in that these files will be deleted when the application is uninstalled, ①however there are some important differences:
**
The platform does not always monitor the space available in external storage, and thus may not automatically delete these files. Currently the only time files here will be deleted by the platform is when running on android.os.Build.VERSION_CODES.JELLY_BEAN_MR1 or later and Environment.isExternalStorageEmulated() returns true. Note that you should be managing the maximum space you will use for these anyway, just like with getCacheDir().
External files are not always available: they will disappear if the user mounts the external storage on a computer or removes it. See the APIs on android.os.Environment for information in the storage state.
**②There is no security enforced with these files. For example, any application holding android.Manifest.permission.WRITE_EXTERNAL_STORAGE can write to these files. **

①和②你可以看下:
①说:以下是与getCacheDir不同点
②说: android.Manifest.permission.WRITE_EXTERNAL_STORAGE 只要有权限的应用都具有写权限。
那可以推导出 谁调用getCacheDir() 谁才能访问。

而从你的描述来看,你调用的是相机,相对于你的webView 来说是另外一个应用,那么 getCacheDir()是相机去调用存储的,因为不了解你的问题,我大体推算出 路径 是 /data/data/XXXX.camera/cache 那么基于上面的分析,webView 所在的应用是不具有访问 相机 缓存的权限。 而getExternalCacheDir 只要有android.Manifest.permission.WRITE_EXTERNAL_STORAGE 权限的应用都具有写权限

没能太理解你遇到的问题,以上不知能不能解决你的疑惑

Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!