Skip to content

Commit eb9f300

Browse files
authored
Fix seekbar preview crashes (#11584)
Fixed crashes from recycled bitmaps by creating real copies of bitmaps if necessary + some minor refactoring
1 parent 035c394 commit eb9f300

1 file changed

Lines changed: 29 additions & 11 deletions

File tree

app/src/main/java/org/schabi/newpipe/player/seekbarpreview/SeekbarPreviewThumbnailHolder.java

Lines changed: 29 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -132,17 +132,8 @@ private void generateDataFrom(final Frameset frameset, final UUID updateRequestI
132132

133133
// Get the bounds where the frame is found
134134
final int[] bounds = frameset.getFrameBoundsAt(currentPosMs);
135-
generatedDataForUrl.put(currentPosMs, () -> {
136-
// It can happen, that the original bitmap could not be downloaded
137-
// In such a case - we don't want a NullPointer - simply return null
138-
if (srcBitMap == null) {
139-
return null;
140-
}
141-
142-
// Cut out the corresponding bitmap form the "srcBitMap"
143-
return Bitmap.createBitmap(srcBitMap, bounds[1], bounds[2],
144-
frameset.getFrameWidth(), frameset.getFrameHeight());
145-
});
135+
generatedDataForUrl.put(currentPosMs,
136+
createBitmapSupplier(srcBitMap, bounds, frameset));
146137

147138
currentPosMs += frameset.getDurationPerFrame();
148139
pos++;
@@ -165,6 +156,33 @@ private void generateDataFrom(final Frameset frameset, final UUID updateRequestI
165156
}
166157
}
167158

159+
private Supplier<Bitmap> createBitmapSupplier(final Bitmap srcBitMap,
160+
final int[] bounds,
161+
final Frameset frameset) {
162+
return () -> {
163+
// It can happen, that the original bitmap could not be downloaded
164+
// (or it was recycled though that should not happen)
165+
// In such a case - we don't want a NullPointer/
166+
// "cannot use a recycled source in createBitmap" Exception -> simply return null
167+
if (srcBitMap == null || srcBitMap.isRecycled()) {
168+
return null;
169+
}
170+
171+
// Cut out the corresponding bitmap form the "srcBitMap"
172+
final Bitmap cutOutBitmap = Bitmap.createBitmap(srcBitMap, bounds[1], bounds[2],
173+
frameset.getFrameWidth(), frameset.getFrameHeight());
174+
175+
// If the cut out bitmap is identical to its source,
176+
// we need to copy the bitmap to create a new instance.
177+
// createBitmap allows itself to return the original object that is was created with
178+
// this leads to recycled bitmaps being returned (if they are identical)
179+
// Reference: https://stackoverflow.com/a/23683075 + first comment
180+
// Fixes: https://github.com/TeamNewPipe/NewPipe/issues/11461
181+
return cutOutBitmap == srcBitMap
182+
? cutOutBitmap.copy(cutOutBitmap.getConfig(), true) : cutOutBitmap;
183+
};
184+
}
185+
168186
@Nullable
169187
private Bitmap getBitMapFrom(final String url) {
170188
if (url == null) {

0 commit comments

Comments
 (0)