Download an android learning File

Source: Internet
Author: User

How do I download network files to a local device? Simply put, it is the same as copying and pasting files locally, that is, copying the content in the files on the network to your local file to download the files.

Andoird ApplicationProgramTo download files, what steps do you need?

1. obtain Internet Access Permissions

To download files from the Internet, you must first be able to access the Internet. If you can access the Internet, you have to grant your application the permission to access the network. You only need to add a row in androidmanifest. xml.CodeYou can do this.<Uses-Permission Android: Name = "android. Permission. Internet"/>Don't ask where I added the file manifest ..

2. Get the httpurlconnection object

How to obtain the httpurlconnection object? To obtain the httpurlconnection object, you must first obtain the URL object of your file. The method isURL myurl = new URL (string URL );The input parameter is the string type URL corresponding to your file. After obtaining the URL object of your file, we can call itsOpenconnection ()To obtain what we needHttpurlconnectionObject.

Httpurlconnection huc = (Httpurlconnection) Myurl.Openconnection ();

3. Get operation permissions for mobile phone sdcard

If you want to download files from the network to a local machine, you must first create a corresponding file locally. since you need to create a file first, you still have to grant the sdcard operation permission for the response to your program. Just like the first step, we only need to add the code for the permission to operate the sdcard.<Uses-Permission Android: Name = "android. Permission. write_external_storage"/>Don't ask where I added it again ..

Iv. Io stream operations

Fuck youCompositionI/O Stream is not required, and the Java basics cannot be supplemented quickly .. now let's talk about the steps. After you set the permissions in step 3, you can access the sdcard directory on your mobile phone.Environment. getexternalstoragedirectory ()This method returns a file object. Generally, we want to operate our own directory under the sdcard directory, so we can add our own directory directly to the end,Environment. getexternalstoragedirectory () + "/mypath"It is best to first use the log. d method to determine whether the directory is the directory you want. If yes, continue the following operations.

I know how to get the sdcard directory, and then we have to create our own files in the directory first. How do I create files? So I won't talk about it. Let's go to the Java basics. After creating the file, we have created the file you want to download in step 2.HttpurlconnectionObject through itsGetinputstream ()You can get the input stream of the corresponding file on the network. Some may ask why the input stream is used? Simply put, the input stream is to input data into the memory. What about the output stream? That is to output the content in the memory to the file. Now that we have obtained the input stream of this file, we can use this input stream for Io operations and write data to our file through the output stream, this allows you to download files!

(PS: You can search for Android. manifest. permission in the official document about the permissions of Android. The attributes of this class are all related permissions.

If garbled characters appear when you operate text files containing Chinese characters, you can check out my other blog http://www.cnblogs.com/Qamefay/archive/2011/09/01/2161495.html.

The URL of the downloaded file must not contain Chinese characters !)

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.