谁能解释一下 Android 中用于下载文件的 File() 参数?
参考 this android 文件下载问题
谁能解释一下这行在
FileOutputStream f = new FileOutputStream(new File(root,"Video.mp4"));
File() 中的参数 root 意味着什么。
我需要指定根路径
来保存文件吗?
如果是这种情况那么我们如何指定 android 中的根路径?
问候
In Reference to this android file download problem
Can anyone explain what does this line mean in the code
FileOutputStream f = new FileOutputStream(new File(root,"Video.mp4"));
And what does it mean by the parameter root within the File()
.
Do I need to specify the root path
to save the file?
If it is the case then how do we specify the root path in android ?
Regards
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您链接的问题中的代码片段没有定义变量,但如果代码将文件下载到设备,我会假设它是 SD 卡上的路径。
Environment.getExternalStorageDirectory()
将为您提供SD卡的根路径。您还需要在清单中指定WRITE_EXTERNAL_STORAGE
权限。如果您正在使用模拟器,则可以在创建模拟器映像时创建虚拟 SD 卡。
The code snippet from the question you linked doesn't define the variable, but if the code is downloading a file to the device, I would assume that it's a path on the SD card.
Environment.getExternalStorageDirectory()
will give you the root path to the SD card. You'll also need to specify theWRITE_EXTERNAL_STORAGE
permission in your manifest.If you're working on the emulator, you can create a virtual SD card when you create the emulator image.
java.io.File(File, String) 或 java.io.File(String, String) 是 Java 的标准 java 构造函数。第一个参数只是父目录路径,第二个参数是实际的文件名。如果文件位于当前工作目录中或者您知道完整路径作为一个字符串,则可以避免使用 2 个参数构造函数。
由于您正在尝试下载文件,因此您只需通过普通的 URL.openStream() 获取文件即可获取 InputStream 来获取下载文件的内容。为了写出数据,您将按照链接到的示例来写内容。
我不确定示例中的 root 变量指向什么。除此之外,我无法帮助您,因为我自己只完成了第一个 Hello,Android 示例。
The java.io.File(File, String) or java.io.File(String, String) are standard java constructors for Java. The first argument is just the parent directory path, while the second is the actual file name. If the file is in the current working directory or you know the full path as one string you can avoid the 2 argument constructors.
Since you are trying to download a file you can just acquire the file through a normal URL.openStream() to get an InputStream to get the contents of your downloaded file. For writing the data out you will follow the example you linked to to write the contents.
I'm unsure what the root variable was pointed to in the example. I'm not able to help you beyond this though since I have only gone through the first Hello, Android example myself.