How to Download an image in ANDROID programatically?
Hello everyone..
In one of my tutorials I have shown you how to upload an image in android..
In todays tutorial I will show you how to download an image into your phone programatically.
I am just picking up an image url from google to show the download.
In one of my tutorials I have shown you how to upload an image in android..
In todays tutorial I will show you how to download an image into your phone programatically.
I am just picking up an image url from google to show the download.
This is the main program that downloads the file.
01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
| package pack.coderzheaven; 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.ByteArrayBuffer; import android.app.Activity; import android.os.Bundle; import android.widget.TextView; public class DownloadImage extends Activity { private final String PATH = "/data/data/pack.coderzheaven/" ; TextView tv; @Override public void onCreate(Bundle savedInstanceState) { super .onCreate(savedInstanceState); setContentView(R.layout.main); tv = (TextView)findViewById(R.id.tv); DownloadFromUrl(PATH+ "dwn_img.jpg" ); } public void DownloadFromUrl(String fileName) { try { URL url = new URL( "http://t3.gstatic.com/images?q=tbn:ANd9GcQs0EPegqi56Alq4vCgC_lVDbZvJtk51RhER7AyDEVA3nUkzjMVK-yDHY3V-w" ); //you can write here any link File file = new File(fileName); long startTime = System.currentTimeMillis(); tv.setText( "Starting download......from " + url); URLConnection ucon = url.openConnection(); InputStream is = ucon.getInputStream(); BufferedInputStream bis = new BufferedInputStream(is); /* * 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); } FileOutputStream fos = new FileOutputStream(file); fos.write(baf.toByteArray()); fos.close(); tv.setText( "Download Completed in" + ((System.currentTimeMillis() - startTime) / 1000 ) + " sec" ); } catch (IOException e) { tv.setText( "Error: " + e); } } } |
AndroidManifest.xml
01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
16
17
18
19
20
| <? xml version = "1.0" encoding = "utf-8" ?> package = "pack.coderzheaven" android:versionCode = "1" android:versionName = "1.0" > < uses-permission android:name = "android.permission.INTERNET" ></ uses-permission > < uses-permission android:name = "android.permission.ACCESS_NETWORK_STATE" ></ uses-permission > < uses-permission android:name = "android.permission.READ_PHONE_STATE" ></ uses-permission > < application android:icon = "@drawable/icon" android:label = "@string/app_name" > < activity android:name = ".DownloadImage" android:label = "@string/app_name" > < intent-filter > < action android:name = "android.intent.action.MAIN" /> < category android:name = "android.intent.category.LAUNCHER" /> </ intent-filter > </ activity > </ application > </ manifest > |
main.xml contents.
01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
16
17
18
19
20
| <? xml version = "1.0" encoding = "utf-8" ?> package = "pack.coderzheaven" android:versionCode = "1" android:versionName = "1.0" > < uses-permission android:name = "android.permission.INTERNET" ></ uses-permission > < uses-permission android:name = "android.permission.ACCESS_NETWORK_STATE" ></ uses-permission > < uses-permission android:name = "android.permission.READ_PHONE_STATE" ></ uses-permission > < application android:icon = "@drawable/icon" android:label = "@string/app_name" > < activity android:name = ".DownloadImage" android:label = "@string/app_name" > < intent-filter > < action android:name = "android.intent.action.MAIN" /> < category android:name = "android.intent.category.LAUNCHER" /> </ intent-filter > </ activity > </ application > </ manifest > |
Strings.xml
1
2
3
4
5
| <? xml version = "1.0" encoding = "utf-8" ?> < resources > < string name = "hello" >DownloadImage Demo from CoderzHeaven</ string > < string name = "app_name" >DownloadImage</ string > </ resources > |
1 comment:
Hi Umamahesh,
congrats for the tutorial!!
I copied it and when execute appears the next message
Error: java.io.FileNotFoundException:/data/data/com.telecoming.griv/dwn_img.jpg (No such file or directory)
Any clue about the issue??
Thank you very much
Post a Comment