-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathASyncTaskDisplayImageHTTP.java
More file actions
37 lines (34 loc) · 1.54 KB
/
ASyncTaskDisplayImageHTTP.java
File metadata and controls
37 lines (34 loc) · 1.54 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
private class ThreadImageFile extends AsyncTask<Integer, Integer, byte[]> {
@Override
protected byte[] doInBackground(Integer... integers) {
String resourceURI = "https://static.wikia.nocookie.net/ptstarwars/images/0/01/Hansoloprofile.jpg/revision/latest/top-crop/width/360/height/450?cb=20120222133702";
byte[] content=null;
try {
URL url = new URL(resourceURI);
HttpURLConnection con = null;
con = (HttpURLConnection) url.openConnection();
con.setRequestMethod("GET");
InputStream is = con.getInputStream();
ByteArrayOutputStream os= new ByteArrayOutputStream();
byte[] buffer = new byte[1024];
int bytesRead;
while ((bytesRead = is.read(buffer)) != -1) {
os.write(buffer, 0, bytesRead);
}
content=os.toByteArray();
}catch(Exception e){
e.printStackTrace();
}
return content;
}
@Override
protected void onPostExecute (byte[] result) {
LinearLayout ll=(LinearLayout)findViewById(R.id.LLItems);
ImageView iv= new ImageView(getBaseContext());
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(250, 250);
iv.setLayoutParams( params);
Bitmap bmp= BitmapFactory.decodeByteArray(result,0,result.length);
iv.setImageBitmap(bmp);
ll.addView(iv);
}
}