I am testing incomplete URLs in AsyncTask to handle missing image and using default drawable for my imageview. I noticed that the stacktrace is printed several times. I worry this is also creating that bitmap over and over again. Is this normal? Any suggestions for handling this better?
Code:
@Override
protected Bitmap doInBackground(String... params) {
Bitmap artistImg = null;
HttpURLConnection urlConnection = null;
try {
URL url = new URL(params[0]);
urlConnection = (HttpURLConnection) url.openConnection();
urlConnection.setRequestMethod("GET");
urlConnection.connect();
InputStream is = urlConnection.getInputStream();
artistImg = BitmapFactory.decodeStream(is);
} catch (FileNotFoundException e) {
e.printStackTrace();
artistImg = BitmapFactory.decodeResource(
context.getResources(), R.drawable.question);
} catch (Exception e) {
Log.v(LOG_TAG, "NETWORK ERROR");
} finally {
if (urlConnection != null) urlConnection.disconnect();
}
return artistImg;
}
Stacktrace (repeated 15+ times):
06-24 10:09:49.354 21523-21567/com.zosocoder.android.spotifystreamer W/System.err﹕ java.io.FileNotFoundException: https://i.scdn.co/image/792a417e8500405
06-24 10:09:49.354 21523-21567/com.zosocoder.android.spotifystreamer W/System.err﹕ at com.android.okhttp.internal.http.HttpURLConnectionImpl.getInputStream(HttpURLConnectionImpl.java:206)
06-24 10:09:49.355 21523-21567/com.zosocoder.android.spotifystreamer W/System.err﹕ at com.android.okhttp.internal.http.DelegatingHttpsURLConnection.getInputStream(DelegatingHttpsURLConnection.java:210)
06-24 10:09:49.355 21523-21567/com.zosocoder.android.spotifystreamer W/System.err﹕ at com.android.okhttp.internal.http.HttpsURLConnectionImpl.getInputStream(HttpsURLConnectionImpl.java:25)
06-24 10:09:49.355 21523-21567/com.zosocoder.android.spotifystreamer W/System.err﹕ at com.zosocoder.android.spotifystreamer.ArtistAdapter$FetchArtistThumbTask.doInBackground(ArtistAdapter.java:85)
06-24 10:09:49.355 21523-21567/com.zosocoder.android.spotifystreamer W/System.err﹕ at com.zosocoder.android.spotifystreamer.ArtistAdapter$FetchArtistThumbTask.doInBackground(ArtistAdapter.java:68)
06-24 10:09:49.355 21523-21567/com.zosocoder.android.spotifystreamer W/System.err﹕ at android.os.AsyncTask$2.call(AsyncTask.java:292)
06-24 10:09:49.355 21523-21567/com.zosocoder.android.spotifystreamer W/System.err﹕ at java.util.concurrent.FutureTask.run(FutureTask.java:237)
06-24 10:09:49.356 21523-21567/com.zosocoder.android.spotifystreamer W/System.err﹕ at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:231)
06-24 10:09:49.356 21523-21567/com.zosocoder.android.spotifystreamer W/System.err﹕ at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
06-24 10:09:49.356 21523-21567/com.zosocoder.android.spotifystreamer W/System.err﹕ at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
06-24 10:09:49.356 21523-21567/com.zosocoder.android.spotifystreamer W/System.err﹕ at java.lang.Thread.run(Thread.java:818)
You might consider using either Picasso or Glide to load your images. It would make your image loading much simpler. Both of those libraries handle loading images in the background/caching already loaded images on disk/specifying default images in case your request fails while retrieving the image.
Loading an image would simply be:
I personally prefer Glide since I find it to be faster but both of those libraries work well and would spare you the trouble of handling your bitmaps manually.