Android的mkdirs()创建了一个文件夹中的零字节的文件,而不是 [英] Android mkdirs() creates a zero byte file instead of a folder
问题描述
在我的Android应用程序,我想对SD卡创建以下文件夹:
In my android application, I am trying to create the following folder on the sdcard:
/mnt/sdcard/OSGiComponents/admin/felix-cache/
这里的code:
Here's the code:
File cacheDir =
new File( Environment.getExternalStorageDirectory().getAbsolutePath() +
"/OSGiComponents/admin/felix-cache/" );
// Create the folder
cacheDir.mkdirs();
// Check if it exists
if ( ! cacheDir.exists() ) {
Log.e ( "Debug" , "Cache directory cannot be created" );
}
我有Android清单文件的清单标签下的WRITE_STORAGE_PERMISSION。我可以创建其他文件夹和文件,而无需对SD卡的问题。 该应用程序工作正常以下电话:
I have the WRITE_STORAGE_PERMISSION under the manifest tag of the android manifest file. I am able to create other folders and files without problem on the sdcard. The app works fine on the following phones:
- 的Nexus S(根)运行姜饼(2.3)
- 的Nexus S(无根)运行果冻豆(4.1.2)
- 的HTC Desire(根)运行升级Froyo(2.2)
- 的HTC Desire(无根)运行升级Froyo(2.2)
不过在三星Galaxy Nexus手机(无根)运行冰淇淋三明治(4.0.4),该目录中创建一个零大小的文件,可在天文可见。的存在()调用返回false。
However on Samsung Galaxy Nexus phone (unrooted) running Ice Cream Sandwich (4.0.4), the directory is created as a zero size file, which can be seen in Astro. The exists() call returns false.
- 你可以从文件夹名称看,我使用的Apache费利克斯。菲利克斯自动创建高速缓存目录,如果它不存在。在Galaxy Nexus的,它总是抱怨说,它无法创建缓存目录。天文显示了一个0字节的文件,而不是一个文件夹。这就是为什么我决定尝试初始化菲利克斯之前创建的缓存文件夹自己。
- 所以,我创建缓存文件夹自己。该应用程序工作正常,第一次,我可以看到该文件夹罚款天文。如果我关闭应用程序,然后删除天文的文件夹,然后重新启动应用程序,甚至我的code神秘无法创建缓存目录,和天文显示了一个0字节的文件。
- 0字节的文件不能在天文删除。然而,当我重新启动手机,文件夹是神奇地出现和确定。
- 在我使用FileInstall观看OSGiComponents / install文件夹。当我把包罐到该文件夹,检测和除了Galaxy Nexus的(当应用程序工作的第一次)所有的手机都安装好。有没有日志/有关无法观看该目录从FileInstall错误。
- 我在2 Galaxy Nexus的手机,同样的问题,测试这一点。
我怀疑这是一个权限问题,但我不知道它是什么,为什么会创建一个0字节的文件,同时存在()返回false。无处可在code我在创建这个文件。
I suspect it is a permissions problem, but I not sure what it is, and why a 0 byte file is created while exists() returns false. Nowhere else in the code am I creating this file.
这可能是什么问题,有什么建议?
Any suggestions on what could be the problem?
感谢:)
更新:我想我已经确定了问题,请参阅我张贴的答案
UPDATE: I think I have identified the issue, please see the answer I posted.
推荐答案
我发现了一个解决方法解决了这个问题。每当我删除,而不是使用删除文件/目录,()直接,我重命名文件/文件夹,然后删除()它。这个奇怪的解决方法似乎删除的问题。
I found a workaround which solves this problem. Whenever I am deleting a file/directory, instead of using delete() directly, I rename the file/folder, and then delete() it. This weird workaround seems to remove the problem.
我通过看这个问题的答案有这样的想法 - 打开失败EBUSY设备或资源忙
I got this idea by seeing this question's answers - Open failed EBUSY device or Resource busy
不过,我不知道为什么这个工程,或者是摆在首位导致的问题。
However, I'm not sure why this works, or what caused the problem in the first place.
在任何情况下,其他人使用的Galaxy Nexus的菲利克斯和遇到同样的问题,只是改变了菲利克斯源$ C $ C,如下所示:
In case anyone else is using Felix on Galaxy Nexus and encounters the same problem, just change the Felix source code as shown below:
org.apache.felix.framework.util.SecureAction.java:
public boolean deleteFile(File target)
{
if (System.getSecurityManager() != null)
{
try
{
Actions actions = (Actions) m_actions.get();
actions.set(Actions.DELETE_FILE_ACTION, target);
return ((Boolean) AccessController.doPrivileged(actions, m_acc))
.booleanValue();
}
catch (PrivilegedActionException ex)
{
throw (RuntimeException) ex.getException();
}
}
else
{
// Solution: Rename before deleting
// http://stackoverflow.com/questions/11539657/open-failed-ebusy-device-or-resource-busy
File to = new File(target.getAbsolutePath() + System.currentTimeMillis());
boolean renameStatus = target.renameTo(to);
boolean deleteStatus = to.delete();
boolean returnStatus = ( renameStatus && deleteStatus );
// Debug SecureAction
//boolean returnStatus = target.delete();
Log.e ( "SecureAction" , "Deleting " + target + " delete(): " + returnStatus );
return returnStatus;
}
}
这篇关于Android的mkdirs()创建了一个文件夹中的零字节的文件,而不是的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!