Android downloads files from a network address, writes to an SD card

Source: Internet
Author: User

First create a Httpdownloader class, get the network address of the downloaded file, download the file and return it as a string stream:

public string Download (string urlstr) {

      URLSTR is the network address of the file, such as Http://192.168.1.105:8080/mp3/resources.xml
StringBuffer sb = new StringBuffer ();
String line = null;
BufferedReader buffer = null;
Try
{
Create a URL object
url = new URL (urlstr);
Create an HTTP connection
HttpURLConnection urlconn = (httpurlconnection) url.openconnection ();
reading data using IO stream
Buffer = new BufferedReader (New InputStreamReader (Urlconn.getinputstream ()));
while (line = Buffer.readline ()) = null) {
Sb.append (line);
}
}
catch (Exception ex) {
Ex.printstacktrace ();
}
finally{
try{
Buffer.close ();
}
catch (Exception ex) {
Ex.printstacktrace ();
}
}
return sb.tostring ();
}
}

Still under this category, define the download file and write to your phone's SD card:

public int Downfile (String urlstr,string path,string filename) {
URLSTR Network, Path: Write to address on SD card, filename download file name
InputStream inputstream = null;
try{
Declaring Fileutils objects
FileUtils FileUtils = new FileUtils ();
if (Fileutils.isfileexist (Filename,path)) {
return 1; Returns 1 if the file already exists
}
else{
Save the download file in the InputStream stream
InputStream = Getinputstreamfromurl (URLSTR);
Write the downloaded file to the SD card
File resultfile = fileutils.write2sdfrominput (path, filename, inputstream);
if (Resultfile = = null) {
return-1; Write Failure returns-1
}
}
}
catch (Exception e) {
E.printstacktrace ();
return-1;
}
finally{
try{
Inputstream.close ();
}
catch (Exception e) {
E.printstacktrace ();
}
}
return 0; Download write to SD card successfully returns 0
}
Public InputStream Getinputstreamfromurl (String urlstr) throws ioexception{
url = new URL (urlstr);
HttpURLConnection urlconn = (httpurlconnection) url.openconnection ();
InputStream InputStream = Urlconn.getinputstream ();
return inputstream;
}

To write the downloaded InputStream to the SD card, define the class Fileutils:

public class FileUtils {
Private String Sdcardroot;

Public FileUtils () {
Get the directory of the current external storage device
Sdcardroot = Environment.getexternalstoragedirectory ()
. GetAbsolutePath ();
}

/**
* Create a file on the SD card
*
* @throws IOException
*/
Public File Createfileinsdcard (string fileName, String dir)
Throws IOException {
File File = new file (sdcardroot + dir + file.separator + fileName);
File.createnewfile ();
return file;
}

/**
* Create a directory on the SD card
*
* @param dirName
*/
Public File Createsddir (String dir) {
File Dirfile = new file (sdcardroot + dir + file.separator);
Dirfile.mkdirs ();
return dirfile;
}

/**
* Determine if the folder on the SD card exists
*/
public boolean isfileexist (string fileName, string path) {
File File = new file (sdcardroot + path + file.separator + fileName);
return file.exists ();
}

/**
* Write data from another inputstream to SD
*/
Public File write2sdfrominput (string path, String fileName,
InputStream input) {
File file = null;
OutputStream output = null;
try {
Createsddir (path);
File = Createfileinsdcard (fileName, path);
The file is written to the stream, that is, the data is written to the file
Output = new FileOutputStream (file);
Defines a buffer size of 4K, that is, every read of 4K content is like writing a file
byte buffer[] = new byte[1024 * 4];
int temp;
while (temp = input.read (buffer))! =-1) {
Output.write (buffer, 0, temp);
}
Output.flush (); Empty cache
} catch (Exception ex) {
Ex.printstacktrace ();
} finally {
try {
Output.close (); Close Write Stream
} catch (Exception ex) {
Ex.printstacktrace ();
}
}
return file;
}
}

Android downloads files from a network address, writes to an SD card

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.