I've got a class called Movie
and within this class I use an AsyncTask
to get a Bitmap
from a url passed in the constructor.
To store the image I use new DownloadImage().execute(url).get();
. However I'm concerned about this blocking the UI Thread as now its a synchronous call. Is there another way I can go about saving the image in the Bitmap
variable?
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.media.Image;
import android.os.AsyncTask;
import android.widget.ImageView;
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.concurrent.ExecutionException;
public class Movie
{
//Variable for a movie
private String name;
private String rating;
private String genre;
private String plot;
private String pathToMovie;
private int movieId;
private int runtime;
private int year;
private Bitmap image;
public Movie(String name, String rating, String genre, String plot, String pathToMovie, int movieId, int runtime, int year, String imagePath) throws ExecutionException, InterruptedException
{
//Set the variables
this.name = name;
this.rating = rating;
this.genre = genre;
this.plot = plot;
this.pathToMovie = pathToMovie;
this.movieId = movieId;
this.runtime = runtime;
this.year = year;
//Download the image from the imagePath
this.image = getImageFromUrl(imagePath);
}
/*
* A function to download the image from the imagePath provided
*/
private Bitmap getImageFromUrl(String url) throws ExecutionException, InterruptedException
{
return new DownloadImage().execute(url).get();
}
//Define getters
public String getName(){ return this.name; }
public String getRating(){ return this.rating; }
public String getGenre(){ return this.genre; }
public String getPlot(){ return this.plot; }
public String getPathToMovie(){ return this.pathToMovie; }
public int getMovieId(){ return this.movieId; }
public int getRuntime(){ return this.runtime; }
public int getYear(){ return this.year; }
public Bitmap getImage(){ return this.image; }
//Set up a AsyncTask to download the images from the url
private class DownloadImage extends AsyncTask<String, Void, Bitmap>
{
@Override
protected Bitmap doInBackground(String... params)
{
//Get the image from the url
try
{
//Create a new URL based on the string provided
URL url = new URL(params[0]);
//Create a new HTTP connection from the URL
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setDoInput(true);
connection.connect();
//Get the content of the page
InputStream inputStream = connection.getInputStream();
//Use a BitmapFactory to convert the InputStream into a bitmap
return BitmapFactory.decodeStream(inputStream);
}
catch (IOException e)
{
e.printStackTrace();
return null;
}
}
}
}
onPostExecute()
or just pass it as a variable topublishProgress()
and overrideonProgressUpdate()
where you use that Bitmap again. Trying to get around this will force you to make the call synchronous. \$\endgroup\$