无法从目录中读取文件 - android
我的项目中有一个目录 /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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您是否已向 AndroidManifest.xml 添加了正确的权限?
另外,明智的做法是使用环境方法来获取正确的 SD 卡路径:
此外,您从以下位置获得什么:
Have you added the right permission to AndroidManifest.xml?
Also, it is wise to use a the environment method to get the correct SD-card path:
Also, what do you get from:
上面提到的/sdcard/audio是在项目目录下创建的一个目录。这种做法是错误的。必须使用 adb 创建模拟 sdcard 的目录(在命令提示符 (windows) 中),如下所示:
adb shell mkdir /sdcard/audio
- 这将在 sdcard/ 中创建 audio/ 子目录就是这样。您应该能够从挂载的 /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:
adb shell mkdir /sdcard/audio
- this will create the audio/ sub-directory in sdcard/adb push "complete path of file name to be pushed" /sdcard/audio/file-name
That's it. You should be able to read files from the mounted /sdcard directory.
@thoredge: thanks for your help.