Merge pull request #9862 from nextcloud/fixLeakedCloseableViolation

Fix LeakedCloseableViolations in getScaledBitmap
This commit is contained in:
Álvaro Brey 2022-02-21 11:36:38 +01:00 committed by GitHub
commit c9018f8ba6
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 22 additions and 43 deletions

View file

@ -1 +1 @@
641 642

View file

@ -106,63 +106,42 @@ public class DiskLruImageCache {
public Bitmap getScaledBitmap(String key, int width, int height) { public Bitmap getScaledBitmap(String key, int width, int height) {
Bitmap bitmap = null; Bitmap bitmap = null;
DiskLruCache.Snapshot snapshot = null;
InputStream inputStream = null;
BufferedInputStream buffIn = null;
String validKey = convertToValidKey(key); String validKey = convertToValidKey(key);
try { try (DiskLruCache.Snapshot snapshot = mDiskCache.get(validKey)) {
snapshot = mDiskCache.get(validKey);
if (snapshot == null) { if (snapshot == null) {
return null; return null;
} }
inputStream = snapshot.getInputStream(0);
if (inputStream != null) {
buffIn = new BufferedInputStream(inputStream, IO_BUFFER_SIZE);
InputStream inputStream = snapshot.getInputStream(0);
if (inputStream != null) {
// First decode with inJustDecodeBounds=true to check dimensions // First decode with inJustDecodeBounds=true to check dimensions
final BitmapFactory.Options options = new BitmapFactory.Options(); final BitmapFactory.Options options = new BitmapFactory.Options();
options.inScaled = true; try (BufferedInputStream buffIn = new BufferedInputStream(inputStream, IO_BUFFER_SIZE)) {
options.inPurgeable = true; options.inScaled = true;
options.inPreferQualityOverSpeed = false; options.inPurgeable = true;
options.inMutable = false; options.inPreferQualityOverSpeed = false;
options.inJustDecodeBounds = true; options.inMutable = false;
options.inJustDecodeBounds = true;
BitmapFactory.decodeStream(buffIn, null, options); BitmapFactory.decodeStream(buffIn, null, options);
}
snapshot = mDiskCache.get(validKey); try (DiskLruCache.Snapshot snapshot2 = mDiskCache.get(validKey)) {
inputStream = snapshot.getInputStream(0); inputStream = snapshot2.getInputStream(0);
buffIn = new BufferedInputStream(inputStream, IO_BUFFER_SIZE);
// Calculate inSampleSize try (BufferedInputStream buffIn = new BufferedInputStream(inputStream, IO_BUFFER_SIZE)) {
options.inSampleSize = BitmapUtils.calculateSampleFactor(options, width, height); // Calculate inSampleSize
options.inSampleSize = BitmapUtils.calculateSampleFactor(options, width, height);
// Decode bitmap with inSampleSize set // Decode bitmap with inSampleSize set
options.inJustDecodeBounds = false; options.inJustDecodeBounds = false;
bitmap = BitmapFactory.decodeStream(buffIn, null, options); bitmap = BitmapFactory.decodeStream(buffIn, null, options);
}
}
} }
} catch (Exception e) { } catch (Exception e) {
Log_OC.e(TAG, e.getMessage(), e); Log_OC.e(TAG, e.getMessage(), e);
} finally {
if (snapshot != null) {
snapshot.close();
}
if (inputStream != null) {
try {
inputStream.close();
} catch (IOException e) {
// nothing to do
}
}
if (buffIn != null) {
try {
buffIn.close();
} catch (IOException e) {
// nothing to do
}
}
} }
if (BuildConfig.DEBUG) { if (BuildConfig.DEBUG) {