Skip to content

Instantly share code, notes, and snippets.

@hu2di
Created April 10, 2017 02:46
Show Gist options
  • Save hu2di/8dc3b8f2aab7f546b6f043975100af30 to your computer and use it in GitHub Desktop.
Save hu2di/8dc3b8f2aab7f546b6f043975100af30 to your computer and use it in GitHub Desktop.
Android: Image Loader - AsyncTask
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