getExternalFilesDir(空)返回null [英] getExternalFilesDir(null) returns null

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

问题描述

再一次,我碰到它已被要求,并回答了之前,但在我实现它仍然没有工作的问题。

Once again, I've come up against a question which has been asked and answered before but in my implementation it is still not working.

我打电话 getExternalFilesDir(空)就在我的主要活动的的onCreate 方式的一开始。它每次返回null,不管我在AVD或我的手机(三星Galaxy加)上运行

I'm calling getExternalFilesDir(null) right at the very start of my main activity's onCreate method. It returns null every time, whether I run it in an AVD or on my phone (Samsung Galaxy Plus).

是的,我有&LT;使用许可权的android:NAME =android.permissions.WRITE_EXTERNAL_STORAG​​E/&gt;在我的的Andr​​oidManifest.xml <行/ STRONG>是的,我检查外部存储状态我拨打电话,并安装了。

Yes, I have the <uses-permission android:name="android.permissions.WRITE_EXTERNAL_STORAGE" /> line in my AndroidManifest.xml and yes, I am checking the external storage state before I make the call and it is mounted.

下面是前三行我的的onCreate()方法内。其实,这只是后的 super.onCreate()的setContentView()来电。

Here are the first three lines inside my onCreate() method. Actually, it's just after the super.onCreate() and setContentView() calls.

String state = Environment.getExternalStorageState();
File extFiles = getExternalFilesDir(null);
File locFiles = getFilesDir();

因此​​,一旦这三行已经执行,这些都为变量的值:

So, once these three lines have executed, these are the values for the variables:

state == "mounted"
extFiles == null
locFiles == "/data/data/com.mypackage.name/files"

有没有人有任何想法,这可能是为什么?

Would anyone have any ideas as to why this might be?

----- -----编辑

所以,我已经试过另一种方法;而不是使用 getExternalFilesDir(空),我尝试使用文件基本路径=新的文件(Environment.getExternalStorageDirectory(),myAppName);

So I've tried another approach; Rather than using getExternalFilesDir(null), I tried using File basePath = new File(Environment.getExternalStorageDirectory(), "myAppName");

这是不理想,我知道Android的文件说,我是同意,你应该宁愿使用 getExternalFilesDir()。眼看因为这不是为我工作,虽然我不得不尝试别的东西。这一次该函数返回一个有效的文件对象,因此,上述行之后,基本路径的路径的/ mnt / SD卡/ myAppName 。到现在为止还挺好。当我与DDMS检查,我可以看到的的/ mnt / SD卡存在,但不是的/ mnt / SD卡/ myAppName 。这是可以预料的。于是我打电话布尔结果= basePath.mkdirs();

This is not ideal and I know that the Android documentation says, and I agree with it, that you should rather use getExternalFilesDir(). Seeing as that's not working for me though I had to try something else. This time the function does return a valid File object so, after the above line, the path of basePath is /mnt/sdcard/myAppName. So far, so good. When I check with DDMS I can see that /mnt/sdcard exists but not /mnt/sdcard/myAppName. This is to be expected. So I call boolean result = basePath.mkdirs();

但是,这将返回false,当我检查文件系统上我可以证实,myAppName子文件夹尚未创建。当我通过DDMS手动创建的文件夹,并把文件在里面,我可以阅读我的​​应用程序这些文件,但我不能写文件夹中的任何东西。

But this returns false and when I check on the file system I can confirm that the myAppName subfolder has not been created. When I create the folder manually through DDMS and put files in it, I can read those files from my application but I can't write anything in that folder.

请帮帮忙!我在我束手无策。

Please help! I'm at my wit's end.

推荐答案

如果当你发布你的问题,这不是一个错字,你可能会恨自己此:

If this wasn't a typo when you posted your question, you'll probably hate yourself for this:

&LT;使用许可权的android:NAME =android.permissions.WRITE_EXTERNAL_STORAG​​E/&GT;

&LT;使用许可权的android:NAME =android.permission.WRITE_EXTERNAL_STORAG​​E/&GT;

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

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