Images are downloaded in Bitmap format.
public class ImageDownloader extends AsyncTask<String,Void,Bitmap>BitmapFactory is used to decode the input stream
// inside doBackground method
URL url = new URL(urls[0]);
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
// we need this extra connect, because we want the input stream to get the image in one go.
connection.connect();
//get input stream
InputStream in = connection.getInputStream();
//now we need bitmap reader
Bitmap myBitmap = BitmapFactory.decodeStream(in);
Downloading image
ImageDownloader task = new ImageDownloader();
Bitmap myImg;
myImg = task.execute("https://upload.wikimedia.org/wikipedia/en/a/aa/Bart_Simpson_200px.png").get();
downloadedImage.setImageBitmap(myImg);
public class MainActivity extends AppCompatActivity {
ImageView downloadedImage;
//use throws on method signature to catch execption if you dont have explicit code inside catch
public void downloadImage(View view) throws ExecutionException, InterruptedException {
ImageDownloader task = new ImageDownloader();
Bitmap myImg;
myImg = task.execute("https://upload.wikimedia.org/wikipedia/en/a/aa/Bart_Simpson_200px.png").get();
downloadedImage.setImageBitmap(myImg);
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
downloadedImage = (ImageView) findViewById(R.id.imageView);
}
public class ImageDownloader extends AsyncTask<String,Void,Bitmap>{
@Override
protected Bitmap doInBackground(String... urls) {
try {
URL url = new URL(urls[0]);
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
// we need this extra connect, because we want the input stream to get the image in one go.
connection.connect();
//get input stream
InputStream in = connection.getInputStream();
//now we need bitmap reader
Bitmap myBitmap = BitmapFactory.decodeStream(in);
return myBitmap;
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
}
}