/
ImageDownloader.java
95 lines (76 loc) · 2.54 KB
/
ImageDownloader.java
1
2
3
4
5
6
7
8
9
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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
package org.smc.silpa.dict;
import java.io.InputStream;
import java.lang.ref.WeakReference;
import java.net.HttpURLConnection;
import java.net.URL;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.AsyncTask;
import android.widget.ImageView;
/**
* Faciliatates Asyncrounous download of Images from the internet.
*
* Heavily borrowed from http://android-developers.blogspot.com/2010/07/multithreading-for-performance.html
*
*/
public class ImageDownloader {
public void download(String word, String lang, ImageView imageView) {
String method = null;
if(lang.equals("en-ml") || lang.equals("en-hi")){
// Normal Image method getimage_def
method = "y";
}else {
// Call wiktionary related method get_wiktionary_image_def
method = "w";
}
String url = "http://silpa.org.in/Dictionary?image="+method+"&text=" + word + "&dict=" + lang + "&imagewidth=0&imageheight=0&fontsize=18";
BitmapDownloaderTask task = new BitmapDownloaderTask(imageView);
task.execute(url);
}
static Bitmap downloadBitmap(String stringUrl) {
URL url = null;
HttpURLConnection connection = null;
InputStream inputStream = null;
try {
url = new URL(stringUrl);
connection = (HttpURLConnection) url.openConnection();
connection.setUseCaches(true);
inputStream = connection.getInputStream();
return BitmapFactory.decodeStream(inputStream);
} catch (Exception e) {
//Log.w(TAG, "Error while retrieving bitmap from " + stringUrl, e);
} finally {
if (connection != null) {
connection.disconnect();
}
}
return null;
}
class BitmapDownloaderTask extends AsyncTask<String, Void, Bitmap> {
private String url;
private final WeakReference<ImageView> imageViewReference;
public BitmapDownloaderTask(ImageView imageView) {
imageViewReference = new WeakReference<ImageView>(imageView);
}
@Override
// Actual download method, run in the task thread
protected Bitmap doInBackground(String... params) {
// params comes from the execute() call: params[0] is the url.
return downloadBitmap(params[0]);
}
@Override
// Once the image is downloaded, associates it to the imageView
protected void onPostExecute(Bitmap bitmap) {
if (isCancelled()) {
bitmap = null;
}
if (imageViewReference != null) {
ImageView imageView = imageViewReference.get();
if (imageView != null) {
imageView.setImageBitmap(bitmap);
imageView.performClick();
}
}
}
}
}