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) {
Bitmap bitmap = null;
DiskLruCache.Snapshot snapshot = null;
InputStream inputStream = null;
BufferedInputStream buffIn = null;
String validKey = convertToValidKey(key);
try {
snapshot = mDiskCache.get(validKey);
try (DiskLruCache.Snapshot snapshot = mDiskCache.get(validKey)) {
if (snapshot == 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
final BitmapFactory.Options options = new BitmapFactory.Options();
options.inScaled = true;
options.inPurgeable = true;
options.inPreferQualityOverSpeed = false;
options.inMutable = false;
options.inJustDecodeBounds = true;
try (BufferedInputStream buffIn = new BufferedInputStream(inputStream, IO_BUFFER_SIZE)) {
options.inScaled = true;
options.inPurgeable = true;
options.inPreferQualityOverSpeed = false;
options.inMutable = false;
options.inJustDecodeBounds = true;
BitmapFactory.decodeStream(buffIn, null, options);
BitmapFactory.decodeStream(buffIn, null, options);
}
snapshot = mDiskCache.get(validKey);
inputStream = snapshot.getInputStream(0);
buffIn = new BufferedInputStream(inputStream, IO_BUFFER_SIZE);
try (DiskLruCache.Snapshot snapshot2 = mDiskCache.get(validKey)) {
inputStream = snapshot2.getInputStream(0);
// Calculate inSampleSize
options.inSampleSize = BitmapUtils.calculateSampleFactor(options, width, height);
try (BufferedInputStream buffIn = new BufferedInputStream(inputStream, IO_BUFFER_SIZE)) {
// Calculate inSampleSize
options.inSampleSize = BitmapUtils.calculateSampleFactor(options, width, height);
// Decode bitmap with inSampleSize set
options.inJustDecodeBounds = false;
bitmap = BitmapFactory.decodeStream(buffIn, null, options);
// Decode bitmap with inSampleSize set
options.inJustDecodeBounds = false;
bitmap = BitmapFactory.decodeStream(buffIn, null, options);
}
}
}
} catch (Exception 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) {