Android Programming :: How To Download File From Internet?

Many times in making an android app you may need to get files from internet and store them on the local android device for later use. For that same purpose you have to download a file and write it on local device.

In order to download a file from internet we make use of the java/apache’s HttpUrlConnection class. To download a file from internet start a new project in eclipse. Add in the following classes which are needed to make a request and to save files on local storage.

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.ArrayList;

The above imports will add in the classes in the project which will be needed to send a request to the server and to download a file and finally to save the file on the local storage. The next step is to create a function which will be called via the thread to download the file.

Why use thread to download a file outside the main UI thread?

The main reason to use a thread outside of main UI thread is that if any activity in an android application takes more then 5 seconds to complete than the android system throws an application not responding exception and show “Force Close” dialog to the user. If your application shows a Force Close to your user, uninstall is almost imminent. So to avoid the same and to let android know you are doing a longer activity we add in a new thread in which we download and save the file.

The following code will be used to download the file:

String imageName = “logo.png”;

String outPath;
outPath = filePath.getPath() + File.separator + imageName;

URL imageUrl = new URL(“http://www.vastal.com/images/logo.png”);

try
{
HttpURLConnection conn = (HttpURLConnection)imageUrl.openConnection();
conn.setConnectTimeout(300000);
conn.setReadTimeout(300000);
conn.setInstanceFollowRedirects(true);

if(conn.getResponseCode() != HttpURLConnection.HTTP_OK)
{
errorOccurred = true;
}

int fileLength = conn.getContentLength();

InputStream is = null;
is = conn.getInputStream();
OutputStream os = new FileOutputStream(outPath);

final int bufferSize=4096;
try
{
byte[] bytes=new byte[bufferSize];
for(;;)
{
int count=is.read(bytes, 0, bufferSize);

if(count==-1)
break;
os.write(bytes, 0, count);

}

errorOccurred = false;
}
catch(Exception ex)
{
ex.printStackTrace();
errorOccurred = true;
}

os.close();
is.close();
}
});
}
catch(Exception e)
{
errorOccurred = true;
}
}
catch(Exception e)
{
errorOccurred = true;
e.printStackTrace();
}

The above code needs to be added in the thread which will be called from the UI thread. The same will download the passed image and store it to the local storage.

We will keep updating our Android Programming Tips section every 4 days, please subscribe to our feed and also share our articles on Facebook and Google Plus so other’s can benefit from our insight. Please feel free to send us your comments below, on how we can improve.

No comments yet.

Leave a Reply