java 清除数据后 getExternalCacheDir() 返回 null
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16562165/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
getExternalCacheDir() returns null after clearing data
提问by Ond?ej Z
I have a simple app that access and writes data to external storage. Everything works fine until I go to Settings -> Apps -> App Info and clear data via "Clear data" button,
then every call to getExternalCacheDir()
starts returning null.
我有一个简单的应用程序,可以访问数据并将数据写入外部存储。一切正常,直到我转到“设置”->“应用程序”->“应用程序信息”并通过“清除数据”按钮清除数据,然后每次调用都getExternalCacheDir()
开始返回空值。
I have been developing on Nexus 7 running Android 4.2.2.
我一直在运行 Android 4.2.2 的 Nexus 7 上进行开发。
My manifest looks like:
我的清单看起来像:
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.package"
android:versionCode="5"
android:versionName="1.3"
xmlns:tools="http://schemas.android.com/tools">
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="17" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.INTERNET" />
<application
...
Code snippet that does not work:
不起作用的代码片段:
Log.d(TAG, "getExternalStorageState() = " + Environment.getExternalStorageState());
Log.d(TAG, "getExternalCacheDir() = " + c.getExternalCacheDir());
Log.d(TAG, "getExternalFilesDir(null) = " + c.getExternalFilesDir(null));
Log.d(TAG, "getExternalFilesDir(Environment.DIRECTORY_MOVIES) = " + c.getExternalFilesDir(Environment.DIRECTORY_MOVIES));
LogCat after the app is installed and executed:
应用程序安装并执行后的LogCat:
05-15 11:26:45.948: DEBUG/HelperUtils(5541): getExternalStorageState() = mounted
05-15 11:26:45.948: DEBUG/HelperUtils(5541): getExternalCacheDir() = /storage/emulated/0/Android/data/com.example.package/cache
05-15 11:26:45.948: DEBUG/HelperUtils(5541): getExternalFilesDir(null) = /storage/emulated/0/Android/data/com.example.package/files
05-15 11:26:45.948: DEBUG/HelperUtils(5541): getExternalFilesDir(Environment.DIRECTORY_MOVIES) = /storage/emulated/0/Android/data/com.example.package/files/Movies
LogCat after clearing data in App Info settings:
在 App Info 设置中清除数据后的 LogCat:
05-15 11:27:57.848: DEBUG/HelperUtils(5859): getExternalStorageState() = mounted
05-15 11:27:57.848: WARN/ContextImpl(5859): Unable to create external cache directory
05-15 11:27:57.848: DEBUG/HelperUtils(5859): getExternalCacheDir() = null
05-15 11:27:57.848: WARN/ContextImpl(5859): Unable to create external files directory
05-15 11:27:57.848: DEBUG/HelperUtils(5859): getExternalFilesDir(null) = null
05-15 11:27:57.848: WARN/ContextImpl(5859): Unable to create external files directory
05-15 11:27:57.848: DEBUG/HelperUtils(5859): getExternalFilesDir(Environment.DIRECTORY_MOVIES) = null
05-15 11:27:57.848: WARN/ContextImpl(5859): Unable to create external cache directory
After clearing data and executing the app getExternalCacheDir()
method returns null even though Environment.getExternalStorageState()
returns "mounted". Does anyone know what could be possibly wrong?
清除数据并执行 appgetExternalCacheDir()
方法后,即使Environment.getExternalStorageState()
返回“已安装”也返回 null 。有谁知道可能有什么问题?
EDIT
编辑
With the help from Gjordis I have found out that Clear databutton removes whole application temporary directory:
在 Gjordis 的帮助下,我发现清除数据按钮会删除整个应用程序临时目录:
storage/sdcard0/Android/data/com.example.app/cache
in Android/data
storage/sdcard0/Android/data/com.example.app/cache
在 Android/data
and I was not able to create it again via getExternalCacheDir()
or manually (though I am able to create other directories under storage/sdcard0/Android/data/
).
我无法通过getExternalCacheDir()
或手动再次创建它(尽管我可以在 下创建其他目录storage/sdcard0/Android/data/
)。
(Android/data/com.example.app
is created again after the device is rebooted, but that is not the solution I am looking for)
(Android/data/com.example.app
在设备重新启动后再次创建,但这不是我正在寻找的解决方案)
回答by Robert
I have encountered and solved this exact problem.
我遇到并解决了这个确切的问题。
Clicking the Clear Data button causes Android to stop your appfrom running and delete the entire application-specific folder "mnt/sdcard/Android/data/your.package.name"
.
单击清除数据按钮会导致 Android 停止您的应用程序运行并删除整个特定于应用程序的文件夹"mnt/sdcard/Android/data/your.package.name"
。
However, I had a separate process that was started from Runtime.getRuntime().exec()
that was still running and it was writing to this folder. This caused the folder to be stuck in a locked state and caused the same symptom you described when my app called getExternalCacheDir()
. Running adb shell
and then ls
from within the /mnt/sdcard/Android/data folder showed that the folder was locked by a process. Running ps
showed that my other process was still running.
但是,我有一个单独的进程从Runtime.getRuntime().exec()
该进程开始,该进程仍在运行并且正在写入该文件夹。这导致文件夹卡在锁定状态并导致您在我的应用程序调用getExternalCacheDir()
. 运行adb shell
然后ls
从 /mnt/sdcard/Android/data 文件夹中显示该文件夹已被进程锁定。运行ps
显示我的另一个进程仍在运行。
The solution was to properly kill the other process that was still writing to my application's application-specific folder before calling getExternalCacheDir()
.
解决方案是在调用getExternalCacheDir()
.
回答by Gjordis
getExternalCacheDir()
returns cache dir like the name says. If there is no cache, there is no directory for it either. This directory is used for temporary files you removed with remove data command. In cases where phone is low on space, it can remove these folders itself too. Atleast some maintenance applications do so.
getExternalCacheDir()
返回缓存目录,如名称所示。如果没有缓存,也没有目录。此目录用于您使用 remove data 命令删除的临时文件。如果手机空间不足,它也可以自行删除这些文件夹。至少一些维护应用程序会这样做。
getExternalFilesDir()
returns the directory for space to save data.
getExternalFilesDir()
返回用于保存数据的空间目录。
回答by Jarvis
If you use getExternal*Cache*Dir(), it is in order to store TEMPORALYdata that can be cleaned by the system. If other applications in front on the backstack needs resources, the systeme can clean your data because system need resources. if you want save your data persistently, use : File file = getExternalFilesDir(null); this is an external memory for storage persitent data (like a virtual sdcard).
如果使用getExternal* Cache*Dir(),是为了存储系统可以清理的TEMPORALY数据。如果后台的其他应用程序需要资源,系统可以清理您的数据,因为系统需要资源。如果要持久保存数据,请使用: File file = getExternalFilesDir(null); 这是用于存储永久数据的外部存储器(如虚拟 SD 卡)。