Created
April 10, 2017 02:46
-
-
Save hu2di/8dc3b8f2aab7f546b6f043975100af30 to your computer and use it in GitHub Desktop.
Android: Image Loader - AsyncTask
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class ImageLoadTask extends AsyncTask<String, Void, Bitmap> { | |
@Override | |
protected Bitmap doInBackground(String... params) { | |
private String url = params[0]; | |
try { | |
//Tiến hành tạo đối tượng URL | |
URL urlConnection = new URL(url); | |
//Mở kết nối | |
HttpURLConnection connection = (HttpURLConnection) urlConnection | |
.openConnection(); | |
connection.setDoInput(true); | |
connection.connect(); | |
//Đọc dữ liệu | |
InputStream input = connection.getInputStream(); | |
//Tiến hành convert qua hình ảnh | |
Bitmap myBitmap = BitmapFactory.decodeStream(input); | |
if(myBitmap==null) | |
return null; | |
return myBitmap; | |
} catch (Exception e) | |
{ | |
e.printStackTrace(); | |
} | |
return null; | |
} | |
@Override | |
protected void onPostExecute(Bitmap result) { | |
super.onPostExecute(result); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment