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

蹲街弑〆低调 提交于 2020-01-06 06:37:28

问题


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


回答1:


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.




回答2:


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.



来源:https://stackoverflow.com/questions/2147704/can-anyone-explain-the-file-parameters-used-to-download-file-in-android

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!