Archive

Posts Tagged ‘download’

Download Files over Internet

August 25th, 2009 No comments

(Android phone – how-to/example)

If you need to download a file on your Android from a remote site, here is one option. First it should be a good idea to compress whatever you store on the internet, thus it will transfer much faster. Given Java/Android have implemented the java.util.zip package, zip should be a very good option for your compression.
(for unzipping your files, it may be a good idea to consult the post on this topic)

You will need two packages:
import java.net.HttpURLConnection;
import java.net.URL;

URL u = new URL(url_filename);
HttpURLConnection c = (HttpURLConnection) u.openConnection();
c.setRequestMethod(”GET”);
c.setDoOutput(true);
c.connect();
InputStream in = c.getInputStream();

// use “in” to save the file locally

Note: for a nice status bar you may use c.getContentLength().

Note2: pay attention that not any type of file may be transferred this way; depending on the remote file type – the HTTP will parse your file, and sometimes issue parsing warnings and errors.

Categories: android java Tags: