-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathImageDownloader.java
More file actions
75 lines (65 loc) · 2.11 KB
/
ImageDownloader.java
File metadata and controls
75 lines (65 loc) · 2.11 KB
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
package mypackage;
import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.AsyncTask;
import android.support.v4.app.FragmentActivity;
import android.util.Log;
import android.widget.ImageView;
import android.widget.TextView;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;
/**
* Created by cscharff.
*/
public class ImageDownloader extends AsyncTask<String, Integer, Bitmap> {
private Activity activity;
public ImageDownloader(FragmentActivity myActivity) {
activity = myActivity;
}
@Override
protected Bitmap doInBackground(String... params) {
Log.i("hello1", "hello1");
publishProgress(1);
try {
URL url = new URL(params[0]);
HttpURLConnection con = (HttpURLConnection) url.openConnection();
if (con.getResponseCode() != HttpURLConnection.HTTP_OK) {
throw new Exception("Failed to connect");
}
InputStream is = con.getInputStream();
publishProgress(0);
Bitmap bitmap = BitmapFactory.decodeStream(is);
is.close();
return bitmap;
} catch (Exception e) {
Log.e("Image", "Failed to load image", e);
Log.e("error", e.getMessage());
}
return null;
}
@Override
protected void onProgressUpdate(Integer... values) {
TextView tv = (TextView) activity.findViewById(R.id.tv_loading);
if (values[0] == 1) {
tv.setText("Loading...");
} else {
tv.setText("");
}
}
@Override
protected void onPostExecute(Bitmap img) {
Log.i("hello4", "hello4");
ImageView iv = (ImageView) activity.findViewById(R.id.remote_image);
Log.i("hello5", "hello5");
if (iv != null && img != null) {
Log.i("hello6", "hello6");
iv.setImageBitmap(img);
}
}
@Override
protected void onPreExecute() {
super.onPreExecute();
}
}