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
-
@Idevicegazette (iDevice Gazette)
GSM-to-Skype bridge lets you lose those roaming fees http://bit.ly/lbRJeh #android
11 years 6 weeks ago -
@tommy_banane (tom b.)
RT @AndroidFavorite: #Android New Desktop Android Market Is Live, Adds Several New Features http://zorr0.nl/lFwXNz
11 years 6 weeks ago -
@dwilliams5 (Dennis Williams)
just completed a runtastic run of 3.02 km in 40 min 11 s with #runtastic #Android App: http://tinyurl.com/5tvrpe3
11 years 6 weeks ago -
@S_Pinz (Spinz!)
RT @Androidheadline: Out of box #LG Optimus 3D got Quadrant 2420 score. Thanks @blink_c #io2011 #android http://twitpic.com/4whkdz
11 years 6 weeks ago -
@tayaitapps (Taya IT)
Next Google TV Looks A Lot Like Android http://t.co/dvlTim3 via @alleyinsider #google #apple #android #tv #honeycomb
11 years 6 weeks ago
Poll
Useful resources
Android Development Projects
- iOS/Android Developer to take older Games and bring them Current
- Android apps developer - need to finish urgent.
- Buliding MobileApp For onlie order
- looking for android APP developers
- Create an ecommerce app
- text-to voice for smartphones IOS - GOOGLE - HARMONY - AND ALEXA
- Optimize Images on App
- Create small feature with drag-drop text for Android
- Scouting for advanced website and Mobile apps developers. Potential Long-term contract.
- BLACK SCREEN