How to download file/image from url to your device


SDK Version: 
M3

We wrote a code for download image from a website to the phone. You can work with images (and files) in your applications, but this article may be useful for java developers too.

Here is the code with comments and logs:

  1. package com.helloandroid.imagedownloader;
  2.  
  3. import java.io.BufferedInputStream;
  4. import java.io.File;
  5. import java.io.FileOutputStream;
  6. import java.io.IOException;
  7. import java.io.InputStream;
  8. import java.net.URL;
  9. import java.net.URLConnection;
  10.  
  11. import org.apache.http.util.ByteArrayBuffer;
  12.  
  13. import android.util.Log;
  14.  
  15. public class ImageManager {
  16.  
  17.         private final String PATH = "/data/data/com.helloandroid.imagedownloader/";  //put the downloaded file here
  18.        
  19.  
  20.         public void DownloadFromUrl(String imageURL, String fileName) {  //this is the downloader method
  21.                 try {
  22.                         URL url = new URL("http://yoursite.com/" + imageURL); //you can write here any link
  23.                         File file = new File(fileName);
  24.  
  25.                         long startTime = System.currentTimeMillis();
  26.                         Log.d("ImageManager", "download begining");
  27.                         Log.d("ImageManager", "download url:" + url);
  28.                         Log.d("ImageManager", "downloaded file name:" + fileName);
  29.                         /* Open a connection to that URL. */
  30.                         URLConnection ucon = url.openConnection();
  31.  
  32.                         /*
  33.                          * Define InputStreams to read from the URLConnection.
  34.                          */
  35.                         InputStream is = ucon.getInputStream();
  36.                         BufferedInputStream bis = new BufferedInputStream(is);
  37.  
  38.                         /*
  39.                          * Read bytes to the Buffer until there is nothing more to read(-1).
  40.                          */
  41.                         ByteArrayBuffer baf = new ByteArrayBuffer(50);
  42.                         int current = 0;
  43.                         while ((current = bis.read()) != -1) {
  44.                                 baf.append((byte) current);
  45.                         }
  46.  
  47.                         /* Convert the Bytes read to a String. */
  48.                         FileOutputStream fos = new FileOutputStream(file);
  49.                         fos.write(baf.toByteArray());
  50.                         fos.close();
  51.                         Log.d("ImageManager", "download ready in"
  52.                                         + ((System.currentTimeMillis() - startTime) / 1000)
  53.                                         + " sec");
  54.  
  55.                 } catch (IOException e) {
  56.                         Log.d("ImageManager", "Error: " + e);
  57.                 }
  58.  
  59.         }
  60. }

You can check the downloaded file in emulator's File Explorer (DDMS):

Last step you should check your application's permissions to use internet:

  1. android.permission.INTERNET
  2. android.permission.ACCESS_NETWORK_STATE
  3. android.permission.READ_PHONE_STATE

Modify the AndroidManifest.xml file like this:

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <manifest xmlns:android="http://schemas.android.com/apk/res/android"
  3.       package="com.helloandroid.imagedownloader"
  4.       android:versionCode="1"
  5.       android:versionName="1.0">
  6.     <application android:icon="@drawable/icon" android:label="@string/app_name">
  7.         <activity android:name=".ImageDownloader"
  8.                   android:label="@string/app_name">
  9.             <intent-filter>
  10.                 <action android:name="android.intent.action.MAIN" />
  11.                 <category android:name="android.intent.category.LAUNCHER" />
  12.             </intent-filter>
  13.         </activity>
  14.  
  15.     </application>
  16.     <uses-sdk android:minSdkVersion="7" />
  17.     <uses-permission android:name="android.permission.INTERNET"></uses-permission>
  18.         <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"></uses-permission>
  19.         <uses-permission android:name="android.permission.READ_PHONE_STATE"></uses-permission>
  20.  
  21. </manifest>

I hope you found this article helpful...