gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-29 02:15:30 26 4
gpt4 key购买 nike

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

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

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

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

下面的代码会导致一个异常,说没有权限,

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

谢谢。我是 Android 新手。

最佳答案

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"
/>

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

关于android File.exists 无法检查 SD 卡上的文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4284436/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com