android File.exists 无法检查 SD 卡上的文件

发布于 2024-10-04 17:05:09 字数 474 浏览 0 评论 0原文

if(new File("/mnt/sdcard/tm").exists()) {...}

当我使用 Eclipse 创建 java 程序并在 Android 手机上调试它时,它测试正确。 但是当我将它放入 Android 系统中的应用程序时,它没有检测到该文件。

而且,在另一个应用程序中(也内置在我的 Android 系统中),尽管我在 AndroidManifest.xml 中添加了以下内容,

下面的代码会引发异常,表示没有权限,

File f=new File("/mnt/sdcard/at"); f.createNewFile();

谢谢。我是安卓新手。

if(new File("/mnt/sdcard/t.m").exists()) {...}

When I create a java program using Eclipse and debug it on my Android phone, it tests right.
But when I put it into a app in my Android system, it does not detect the file.

And, in another app (built in my Android system, too), though I've put following in AndroidManifest.xml,

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>

the code below causes an exception saying no permission,

File f=new File("/mnt/sdcard/a.t"); f.createNewFile();

Thank you. I'm new to Android.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

拔了角的鹿 2024-10-11 17:05:09

写入SD卡的权限中:

你说得对,SD卡
目录是 /sdcard 但你不应该
对其进行硬编码。相反,做一个
打电话给
环境.getExternalStorageDirectory()
获取目录:

文件 sdDir =
环境.getExternalStorageDirectory();
如果您还没有这样做,那么您
需要给你的应用程序正确的
写入 SD 卡的权限
将其添加到您的清单中:

 <uses-permission
  android:name="android.permission.WRITE_EXTERNAL_STORAGE"
  />

只需将其更改为读取(而不是写入)

in Permission to write to the SD card:

You're right that the SD Card
directory is /sdcard but you shouldn't
be hard coding it. Instead, make a
call to
Environment.getExternalStorageDirectory()
to get the directory:

File sdDir =
Environment.getExternalStorageDirectory();
If you haven't done so already, you
will need to give you app the correct
permission to write to the SD Card by
adding this to your Manifest:

 <uses-permission
  android:name="android.permission.WRITE_EXTERNAL_STORAGE"
  />

Just change it to read (instead of write)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文