How to download file/image from url to your device
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:
- package com.helloandroid.imagedownload
er; - import java.io.BufferedInputStream;
- import java.io.File;
- import java.io.FileOutputStream;
- import java.io.IOException;
- import java.io.InputStream;
- import java.net.URL;
- import java.net.URLConnection;
- import org.apache.http.util.ByteArray
Buffer; - import android.util.Log;
- public class ImageManager {
- private final String PATH = "/data/data/com.helloandr
oid.imagedownloader/"; //put the downloaded file here - try {
- Log.d("ImageManager", "download begining");
- Log.d("ImageManager", "download url:" + url);
- Log.d("ImageManager", "downloaded file name:" + fileName);
- /* Open a connection to that URL. */
- /*
- * Define InputStreams to read from the URLConnection.
- */
- /*
- * Read bytes to the Buffer until there is nothing more to read(-1).
- */
- ByteArrayBuffer baf = new ByteArrayBuffer(50);
- int current = 0;
- while ((current = bis.read()) != -1) {
- baf.append((byte) current);
- }
- /* Convert the Bytes read to a String. */
- fos.write(baf.toByteArray());
- fos.close();
- Log.d("ImageManager", "download ready in"
- + " sec");
- Log.d("ImageManager", "Error: " + e);
- }
- }
- }
You can check the downloaded file in emulator's File Explorer (DDMS):

Last step you should check your application's permissions to use internet:
- android.permission.INTERNET
- android.permission.ACCESS_NETWORK_STATE
- android.permission.READ_PHONE_STATE
Modify the AndroidManifest.xml file like this:
- <?xml version="1.0" encoding="utf-8"?>
- <manifest xmlns:android="http://schemas.android.c
om/apk/res/android" - package="com.helloandroid.imagedo
wnloader" - android:versionCode="1"
- android:versionName="1.0">
- <application android:icon="@drawable/icon" android:label="@string/app_name">
- <activity android:name=".ImageDownloader"
- android:label="@string/app_name">
- <intent-filter>
- <action android:name="android.intent.action.MA
IN" /> - <category android:name="android.intent.category.
LAUNCHER" /> - </intent-filter>
- </activity>
- </application>
- <uses-sdk android:minSdkVersion="7" />
- <uses-permission android:name="android.permission.INTER
NET"></uses-permission> - <uses-permission android:name="android.permission.ACCES
S_NETWORK_STATE"></uses-permission> - <uses-permission android:name="android.permission.READ_
PHONE_STATE"></uses-permission> - </manifest>
I hope you found this article helpful...
New tutorials from Helloandroid
Recent Apps
Android on Twitter
-
@StephanieNich10 (Stephanie Nichols)#android I laughed so hard at ochocinco 's avi. Hahahaha http://t.co/MPuhhi3m
25 weeks 2 days ago -
@CarlaAtkins8 (Carla Atkins)#android Omg! This is actually f'n interesting http://t.co/JodMOehr
25 weeks 2 days ago -
@MarianMcleod12 (Marian Mcleod)#android Precisely what song is? http://t.co/YmJXU0rB
25 weeks 2 days ago -
@JoBeach15 (Jo Beach)#android haha this made me laugh, i love ted:-) http://t.co/gtcWQ79C
25 weeks 2 days ago -
@aochart3 (青ちゃ)Start playing Paradise Island on Android http://t.co/DEID0Ao5 #Android #Androidgames #Gameinsight http://t.co/e1bifSeL
25 weeks 2 days ago
Poll
Useful resources
Android Development Projects
- Slot Game App - Mobile Platform by UshaKiranmayee1
- Android OpenVPN App by majid2106
- Anrdoird app test with results by krokslin
- Faisalabad Based Wordpress and Android Developer required by sitarainfotech
- Basic Android Apps by robindutt16
- Write dojo UI components for use in an Android app - repost by edk5720
- Skout similar for experts IOS and Android Developers only please let your bid be final, social app - repost 4 by expemployer
- Iphone, Ipad and Android application by kettaneh1
- Mobile Forms Customization (FormMobi & Javascript) - repost by hamishmcgovern
- NFC Tools App by Cponseti92



