无法从目录中读取文件 - android

发布于 2024-10-12 03:39:00 字数 1041 浏览 2 评论 0原文

我的项目中有一个目录 /sdcard/audio 。该目录包含一些音频(wav)文件。
这是我正在尝试做的事情:
1.从目录中读取。
2. 播放wav 文件。

我编写的用于访问目录的代码如下:

String path_to_media = "/sdcard/audio/";
File dirEffectFiles = new File(path_to_media);   //gets a handle to the directory of effect files.
Log.v(this.toString(), "Getting into path = " + path_to_media);
Log.v(this.toString(), "Some details about the directory: path = " + dirEffectFiles.getPath()
                + " Can read: " + dirEffectFiles.canRead() + " list:" + dirEffectFiles.list()
                + " absolute path:" + dirEffectFiles.getAbsolutePath() + " is absolute:" + dirEffectFiles.isAbsolute()); 

我得到的上述输出是:

01-13 16:23:34.941:有关的一些详细信息 目录:路径=/sdcard/audio 可以读取: false 列表:null 绝对值 路径:/sdcard/audio 是绝对的:true

我有以下问题:
1. 如何使上述文件夹可读?通过设置dirEffectFiles.setReadable(true)
2. 另外,请注意上面的path_to_media是/sdcard/audio/。打印时,最后一个“/”消失。这是某些内部解析的正常效果还是我应该担心?

非常欢迎任何帮助,
谢谢,
斯里拉姆。

I have a directory /sdcard/audio in the project. This directory contains some audio (wav) files.
Here is what I am trying to do:
1. Read from the directory.
2. Play the wav files.

The code I have written for accessing the directory is as follows:

String path_to_media = "/sdcard/audio/";
File dirEffectFiles = new File(path_to_media);   //gets a handle to the directory of effect files.
Log.v(this.toString(), "Getting into path = " + path_to_media);
Log.v(this.toString(), "Some details about the directory: path = " + dirEffectFiles.getPath()
                + " Can read: " + dirEffectFiles.canRead() + " list:" + dirEffectFiles.list()
                + " absolute path:" + dirEffectFiles.getAbsolutePath() + " is absolute:" + dirEffectFiles.isAbsolute()); 

The output I get for the above is:

01-13 16:23:34.941: Some details about
the directory: path = /sdcard/audio
Can read: false list:null absolute
path:/sdcard/audio is absolute:true

I have the following questions:
1. How can I make the above folder readable? By setting dirEffectFiles.setReadable(true)?
2. Also, please note that path_to_media above is /sdcard/audio/. When it is printed, the last "/" disappears. Is that a normal effect of some internal parsing or should I be worried about that??

Any help is most welcome,
Thanks,
Sriram.

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

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

发布评论

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

评论(2

眉目亦如画i 2024-10-19 03:39:00

您是否已向 AndroidManifest.xml 添加了正确的权限?

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

另外,明智的做法是使用环境方法来获取正确的 SD 卡路径:

Environment.getExternalStorageDirectory()

此外,您从以下位置获得什么:

dirEffectFiles.exists()

Have you added the right permission to AndroidManifest.xml?

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

Also, it is wise to use a the environment method to get the correct SD-card path:

Environment.getExternalStorageDirectory()

Also, what do you get from:

dirEffectFiles.exists()
三生路 2024-10-19 03:39:00

上面提到的/sdcard/audio是在项目目录下创建的一个目录。这种做法是错误的。必须使用 adb 创建模拟 sdcard 的目录(在命令提示符 (windows) 中),如下所示:

  1. adb shell mkdir /sdcard/audio - 这将在 sdcard/ 中创建 audio/ 子目录
  2. < code>adb push "要推送的文件名的完整路径" /sdcard/audio/file-name
  3. 对于其他波形文件重复此过程。

就是这样。您应该能够从挂载的 /sdcard 目录中读取文件。
@thoredge:感谢您的帮助。

/sdcard/audio referred to above was a directory made in the project directory. This approach is wrong. The directory for emulating the sdcard must be created using adb (in command prompt (windows))like so:

  1. adb shell mkdir /sdcard/audio - this will create the audio/ sub-directory in sdcard/
  2. adb push "complete path of file name to be pushed" /sdcard/audio/file-name
  3. This procedure is repeated for the other wavefiles.

That's it. You should be able to read files from the mounted /sdcard directory.
@thoredge: thanks for your help.

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