getExternalCacheDir()返回清除数据后空 [英] getExternalCacheDir() returns null after clearing data

查看:2217
本文介绍了getExternalCacheDir()返回清除数据后空的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有访问和写入数据到外部存储一个简单的应用程序。一切工作正常,直到我进入设置 - >应用程序 - >应用信息,并通过清除数据按钮清晰的数据, 然后每次调用 getExternalCacheDir()开始返回null。

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.

我一直在开发上的Nexus 7运行Android 4.2.2。

I have been developing on Nexus 7 running Android 4.2.2.

我的表现是这样的:

<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段不工作:

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中:

LogCat after the app is installed and executed:

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中:

LogCat after clearing data in App Info settings:

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

在清理数据和执行应用程序 getExternalCacheDir()返回null即使 Environment.getExternalStorageState()返回安装。有谁知道什么可能是可能是错的?

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?

修改

通过从Gjordis的帮助下我已经发现了的清除数据的按钮,删除整个应用程序的临时目录:

With the help from Gjordis I have found out that Clear data button removes whole application temporary directory:

存储/ sdcard0 /安卓/数据/ com.example.app /缓存的Andr​​oid /数据

和我是不是能够通过 getExternalCacheDir()或手动重新创建它(尽管我可以在存储/ sdcard0创建其他目录/安卓/数据/ )。

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/).

的Andr​​oid /数据/ com.example.app <​​/ code>再次创建设备重新启动后,但是这不是我要找的解决方案)

(Android/data/com.example.app is created again after the device is rebooted, but that is not the solution I am looking for)

推荐答案

我也遇到和解决了这个确切的问题。点击清除数据按钮,使机器人停止你的应用程序停止运行,删除整个应用程序特定的文件夹MNT / SD卡/安卓/数据/ your.package.name。不过,我有一个从调用Runtime.getRuntime()启动一个单独的进程。EXEC()这是仍在运行,它被写入该文件夹。这导致被卡在锁定状态的文件夹,并造成你描述的时候,我的应用程序称为getExternalCacheDir()相同的症状。运行亚行外壳然后 LS 从/ mnt / SD卡/安卓/ data文件夹中显示该文件夹被锁定通过一个过程。运行 PS 显示,我的其他程序仍在运行。

I have encountered and solved this exact problem. Clicking the Clear Data button causes Android to stop your app from running and delete the entire application-specific folder "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.

解决的办法是在调用getExternalCacheDir()。

The solution was to properly kill the other process that was still writing to my application's application-specific folder before calling getExternalCacheDir().

这篇关于getExternalCacheDir()返回清除数据后空的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆