我写了一个缓存系统。它使用平台/ Android方法将数据保存到App的缓存文件夹中以查找文件夹。我有一个问题,我无法在Apps缓存文件夹中创建新文件夹。
此错误在某些设备的Crashlytics(通过非致命错误报告)中报告,而对于大多数不是。
包含该问题的方法是我的getCacheFolder()
方法。
在这些设备上无效的示例文件夹路径:/storage/emulated/0/Android/data/app.package.name/cache/stream-cache
。
受影响的报告版本:4.4.2
; 5.0.1
; 6.0
,6.0.1
。
受影响的报告设备:LENOVO YOGA Tablet 2-1050L
,MOTOROLA MotoE2(4G-LTE)
,Samsung SM-N910F
(报告的故障占98%)。
我怀疑可能存在与可移动sdcards相关的问题。
@NonNull
public File getCacheFolder() {
// If this is the first run of this library function, we should get our cache folder
if (mCacheFolder == null || !mCacheFolder.exists()) {
File dir = null;
// Prioritise the external sdcard to store cached files
dir = mContext.get().getExternalCacheDir();
if(dir == null) {
// There was no external sdcard - instead use internal storage
dir = mContext.get().getCacheDir();
}
// Still couldn't get a cache location, go to fallback plan and throw exception
if(dir == null) {
throw new IllegalStateException("Could not create a location to cache. " +
"This leaves the caching library in a bad state.");
}
// Point to our specific caching folder for this system
mCacheFolder = new File(dir, "stream-cache");
}
// If it doesn't exist, we will need to make it
if (!mCacheFolder.exists()) {
// If we cannot make the directory, go to fallback plan and throw an exception
if (!mCacheFolder.mkdirs()) {
// HERE IS WHERE THE FAULT IS REPORTED
throw new IllegalStateException("Could not create a location to cache. " +
"This leaves the caching library in a bad state.: "+mCacheFolder);
}
}
return mCacheFolder;
}
我更新了代码以写出有关状态的各种信息:
@NonNull
public File getCacheFolder() {
if (mCacheFolder == null || !mCacheFolder.exists()) {
File dir = mContext.get().getExternalCacheDir();
if(dir == null || !Utils.isExternalStorageWritable()) {
dir = mContext.get().getCacheDir();
}
mCacheFolder = new File(dir, "stream-cache");
}
if (!mCacheFolder.exists()) {
if (!mCacheFolder.mkdirs()) {
// Problems writing to cache - test writing to file path.
File filePath = mContext.get().getExternalFilesDir(null);
File cachePath = new File(filePath, "stream-cache");
throw new IllegalStateException("Could not create a location to cache. " +
"This leaves the caching library in a bad state.: "+mCacheFolder+ ". " +
"filePath="+filePath+". " +
"cachePath="+cachePath+". " +
"cachePath-exists="+cachePath.exists()+". " +
"cachePath-mkdirs="+cachePath.mkdirs());
}
}
return mCacheFolder;
}
由此产生的重要结果:
filePath = null - Context.getExternalFilesDir(null);
返回null。打破其余的测试。
答案 0 :(得分:0)
而不是使用
// Point to our specific caching folder for this system
mCacheFolder = new File(dir, "stream-cache");
尝试使用:
// Point to our specific caching folder for this system
mCacheFolder = new File(dir + "/stream-cache");
在您的第一个代码段中。