1 |
/**
|
2 |
* aagtl Advanced Geocaching Tool for Android
|
3 |
* loosely based on agtl by Daniel Fett <fett@danielfett.de>
|
4 |
* Copyright (C) 2010 - 2012 Zoff <aagtl@work.zoff.cc>
|
5 |
*
|
6 |
* This program is free software; you can redistribute it and/or
|
7 |
* modify it under the terms of the GNU General Public License
|
8 |
* version 2 as published by the Free Software Foundation.
|
9 |
*
|
10 |
* This program is distributed in the hope that it will be useful,
|
11 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
12 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
13 |
* GNU General Public License for more details.
|
14 |
*
|
15 |
* You should have received a copy of the GNU General Public License
|
16 |
* along with this program; if not, write to the
|
17 |
* Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
|
18 |
* Boston, MA 02110-1301, USA.
|
19 |
*/
|
20 |
|
21 |
package com.zoffcc.applications.aagtl;
|
22 |
|
23 |
import java.io.BufferedInputStream;
|
24 |
import java.io.File;
|
25 |
import java.io.FileOutputStream;
|
26 |
import java.io.IOException;
|
27 |
import java.io.InputStream;
|
28 |
import java.net.SocketTimeoutException;
|
29 |
import java.net.URL;
|
30 |
import java.net.URLConnection;
|
31 |
|
32 |
import org.apache.http.util.ByteArrayBuffer;
|
33 |
|
34 |
import android.util.Log;
|
35 |
|
36 |
public class ImageManager
|
37 |
{
|
38 |
public void DownloadFromUrl(String imageURL, String fileName)
|
39 |
{ // this is
|
40 |
// the downloader method
|
41 |
try
|
42 |
{
|
43 |
URL url = new URL(imageURL);
|
44 |
File file = new File(fileName);
|
45 |
|
46 |
//long startTime = System.currentTimeMillis();
|
47 |
//Log.d("ImageManager", "download begining");
|
48 |
//Log.d("ImageManager", "download url:" + url);
|
49 |
//Log.d("ImageManager", "downloaded file name:" + fileName);
|
50 |
/* Open a connection to that URL. */
|
51 |
URLConnection ucon = url.openConnection();
|
52 |
ucon.setConnectTimeout(10000);
|
53 |
ucon.setReadTimeout(7000);
|
54 |
|
55 |
/*
|
56 |
* Define InputStreams to read from the URLConnection.
|
57 |
*/
|
58 |
InputStream is = ucon.getInputStream();
|
59 |
BufferedInputStream bis = new BufferedInputStream(is, HTMLDownloader.large_buffer_size);
|
60 |
|
61 |
/*
|
62 |
* Read bytes to the Buffer until there is nothing more to read(-1).
|
63 |
*/
|
64 |
ByteArrayBuffer baf = new ByteArrayBuffer(HTMLDownloader.default_buffer_size);
|
65 |
int current = 0;
|
66 |
while ((current = bis.read()) != -1)
|
67 |
{
|
68 |
baf.append((byte) current);
|
69 |
}
|
70 |
|
71 |
/* Convert the Bytes read to a String. */
|
72 |
FileOutputStream fos = new FileOutputStream(file);
|
73 |
fos.write(baf.toByteArray());
|
74 |
fos.close();
|
75 |
//Log.d("ImageManager", "download ready in"
|
76 |
// + ((System.currentTimeMillis() - startTime) / 1000)
|
77 |
// + " sec");
|
78 |
|
79 |
}
|
80 |
catch (SocketTimeoutException e2)
|
81 |
{
|
82 |
Log.d("ImageManager", "Connectiont timout: " + e2);
|
83 |
}
|
84 |
catch (IOException e)
|
85 |
{
|
86 |
Log.d("ImageManager", "Error: " + e);
|
87 |
}
|
88 |
catch (Exception e)
|
89 |
{
|
90 |
Log.d("ImageManager", "Error: " + e);
|
91 |
}
|
92 |
|
93 |
}
|
94 |
|
95 |
}
|