Skip to content

Commit

Permalink
Закрывать подключение после загрузки картинки или в случае неудачи
Browse files Browse the repository at this point in the history
  • Loading branch information
sadr0b0t committed Oct 3, 2020
1 parent 6d67146 commit 4b54211
Showing 1 changed file with 24 additions and 20 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -61,12 +61,26 @@ private VideoThumbManager() {
private Bitmap defaultThumb = null;//BitmapFactory.decodeResource(R.drawable.bug1);

public Bitmap loadBitmap(final String url) throws IOException {
// 2020-10-03 22:14:23.911 27270-27314/su.sadrobot.yashlang W/OkHttpClient: A connection to https://i.ytimg.com/ was leaked. Did you forget to close a response body?

HttpURLConnection connection = (HttpURLConnection) new URL(url).openConnection();
// если здесь будет IOException в openConnection или в connect, то вылетит весь метод
final HttpURLConnection connection = (HttpURLConnection) new URL(url).openConnection();
connection.connect();
InputStream input = connection.getInputStream();

return BitmapFactory.decodeStream(input);
InputStream input = null;
Bitmap bm;

try {
input = connection.getInputStream();
bm = BitmapFactory.decodeStream(input);
} finally {
if(input != null) {
input.close();
}
connection.disconnect();
}

return bm;
}

/**
Expand Down Expand Up @@ -107,39 +121,29 @@ public Bitmap loadVideoThumb(final String ytId) throws IOException {
* @return
*/
public Bitmap loadVideoThumb(final Context context, final VideoItem vid) {
Bitmap thumb;
Bitmap thumb = null;
try {
//thumb = loadVideoThumb(vid.getYtId());
thumb = loadBitmap(vid.getThumbUrl());
} catch (IOException e) {
//thumb = defaultThumb; // default thumb
thumb = BitmapFactory.decodeResource(context.getResources(), R.drawable.ic_yashlang_thumb);
//e.printStackTrace();
}
return thumb;
}

/**
* Загрузить картинку с превью видео или вернуть картинку по умолчанию.
* @param thumbUrl
* @return
*/
public Bitmap loadVideoThumb(final Context context, final String thumbUrl) {
Bitmap thumb;
try {
thumb = loadBitmap(thumbUrl);
} catch (IOException e) {
//thumb = defaultThumb; // default thumb
if(thumb == null) {
thumb = BitmapFactory.decodeResource(context.getResources(), R.drawable.ic_yashlang_thumb);
}
return thumb;
}


public Bitmap loadPlaylistThumb(final Context context, final String thumbUrl) {
Bitmap thumb;
Bitmap thumb = null;
try {
thumb = loadBitmap(thumbUrl);
} catch (IOException e) {
//thumb = defaultThumb; // default thumb
}
if(thumb == null) {
thumb = BitmapFactory.decodeResource(context.getResources(), R.drawable.ic_yashlang_thumb);
}
return thumb;
Expand Down

0 comments on commit 4b54211

Please sign in to comment.