context.getExternalFilesDir(空)为空 [英] context.getExternalFilesDir(null) is null

查看:6433
本文介绍了context.getExternalFilesDir(空)为空的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图调试我在我的应用程序有问题。问题是,getExternalFiles(DIR)返回null在我的Andr​​oid应用程序,但我不知道为什么。我有必要的权限。这是存在于多种设备和Android版本(2.3.5及以上)的错误。让我知道如果你需要任何更多的信息。

I am trying to debug a problem I am having in my app. The issue is that getExternalFiles(dir) returns null in my android app but I am not sure why. I have the necessary permissions. This is a bug which exists on multiple devices and versions of android (2.3.5 and up). Let me know if you need any more information.

code的线,是问题是:

The line of code which is the problem is:

路径= context.getExternalFilesDir(空)+/database/mydata.db;

path = context.getExternalFilesDir(null) + "/database/mydata.db";

路径= context.getFilesDir()getAbsolutePath()+/database/mydata.db。

path = context.getFilesDir().getAbsolutePath() + "/database/mydata.db";

感谢您。

推荐答案

context.getExternalFilesDir(空)返回是一个完全有效的响应。

context.getExternalFilesDir(null) returning null is a perfectly valid response.

从文档:

返回

在外部存储的目录控股应用程序文件的路径。返回如果外部存储当前未安装,从而它不能保证路径存在;你将需要可用时再调用这个方法。

The path of the directory holding application files on external storage. Returns null if external storage is not currently mounted so it could not ensure the path exists; you will need to call this method again when it is available.

<一个href=\"http://developer.android.com/reference/android/content/Context.html#getExternalFilesDir(java.lang.String)\" rel=\"nofollow\">http://developer.android.com/reference/android/content/Context.html#getExternalFilesDir(java.lang.String)

所以看起来它失败有一个外接SD卡目前无法使用(用户安装它,删除它,等)的设备。

So it looks like the devices where it is failing have an external SD card that is currently not available (user has mounted it, removed it, etc.).

您必须检查这种情况下,使用类似 Environment.getExternalStorageState()和相应的处理,如果它不存在。

You will have to check for this case using something like Environment.getExternalStorageState() and handle accordingly if it's not there.

这篇关于context.getExternalFilesDir(空)为空的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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