开发者

Can anyone explain the File() parameters used to download file in android?

开发者 https://www.devze.com 2022-12-18 01:55 出处:网络
In Reference to this android file download problem Can anyone explain what does this line mean in the code

In Reference to this android file download problem

Can anyone explain what does this line mean in the code

FileOu开发者_如何学运维tputStream 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


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?

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 the WRITE_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.


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号