Skip to content

Commit 907fdc5

Browse files
Merge pull request #14823 from nextcloud/handle-sample-bitmap-decoding-better
Handle Sample Bitmap Decoding Better
2 parents c0b0be1 + 2278c74 commit 907fdc5

File tree

1 file changed

+41
-12
lines changed

1 file changed

+41
-12
lines changed

app/src/main/java/com/owncloud/android/utils/BitmapUtils.java

+41-12
Original file line numberDiff line numberDiff line change
@@ -35,15 +35,16 @@
3535
import com.owncloud.android.lib.resources.users.StatusType;
3636
import com.owncloud.android.ui.StatusDrawable;
3737

38-
3938
import java.io.File;
39+
import java.io.IOException;
4040
import java.nio.charset.StandardCharsets;
4141
import java.security.MessageDigest;
4242
import java.security.NoSuchAlgorithmException;
4343
import java.util.Locale;
4444

4545
import androidx.annotation.NonNull;
4646
import androidx.annotation.Nullable;
47+
import androidx.annotation.RequiresApi;
4748
import androidx.core.graphics.drawable.RoundedBitmapDrawable;
4849
import androidx.core.graphics.drawable.RoundedBitmapDrawableFactory;
4950
import androidx.exifinterface.media.ExifInterface;
@@ -75,6 +76,28 @@ public static Bitmap addColorFilter(Bitmap originalBitmap, int filterColor, int
7576
return resultBitmap;
7677
}
7778

79+
@Nullable
80+
@RequiresApi(Build.VERSION_CODES.P)
81+
private static Bitmap decodeSampledBitmapViaImageDecoder(@NonNull File file, int reqWidth, int reqHeight) {
82+
try {
83+
Log_OC.i(TAG, "Decoding Bitmap via ImageDecoder");
84+
85+
final var imageDecoderSource = ImageDecoder.createSource(file);
86+
87+
final var onDecoderListener = new ImageDecoder.OnHeaderDecodedListener() {
88+
@Override
89+
public void onHeaderDecoded(@NonNull ImageDecoder decoder, @NonNull ImageDecoder.ImageInfo info, @NonNull ImageDecoder.Source source) {
90+
decoder.setTargetSize(reqWidth, reqHeight);
91+
}
92+
};
93+
94+
return ImageDecoder.decodeBitmap(imageDecoderSource, onDecoderListener);
95+
} catch (IOException exception) {
96+
Log_OC.w(TAG, "Decoding Bitmap via ImageDecoder failed, BitmapFactory.decodeFile will be used");
97+
return null;
98+
}
99+
}
100+
78101
/**
79102
* Decodes a bitmap from a file containing it minimizing the memory use, known that the bitmap will be drawn in a
80103
* surface of reqWidth x reqHeight
@@ -84,19 +107,23 @@ public static Bitmap addColorFilter(Bitmap originalBitmap, int filterColor, int
84107
* @param reqHeight Height of the surface where the Bitmap will be drawn on, in pixels.
85108
* @return decoded bitmap
86109
*/
110+
@Nullable
87111
public static Bitmap decodeSampledBitmapFromFile(String srcPath, int reqWidth, int reqHeight) {
112+
final var file = new File(srcPath);
113+
if (!file.exists()) {
114+
Log_OC.e(TAG, "File does not exists, returning null");
115+
return null;
116+
}
117+
88118
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P) {
89-
// For API 28 and above, use ImageDecoder
90-
try {
91-
return ImageDecoder.decodeBitmap(ImageDecoder.createSource(new File(srcPath)),
92-
(decoder, info, source) -> {
93-
// Set the target size
94-
decoder.setTargetSize(reqWidth, reqHeight);
95-
});
96-
} catch (Exception exception) {
97-
Log_OC.e("BitmapUtil", "Error decoding the bitmap from file: " + srcPath + ", exception: " + exception.getMessage());
119+
final var result = decodeSampledBitmapViaImageDecoder(file, reqWidth, reqHeight);
120+
if (result != null) {
121+
return result;
98122
}
99123
}
124+
125+
Log_OC.i(TAG, "Decoding Bitmap via BitmapFactory.decodeFile");
126+
100127
// set desired options that will affect the size of the bitmap
101128
final Options options = new Options();
102129

@@ -134,8 +161,10 @@ public static Bitmap retrieveBitmapFromFile(String storagePath, int minWidth, in
134161
originalHeight = tempWidth;
135162
}
136163

137-
var bitmapResult = decodeSampledBitmapFromFile(
138-
storagePath, originalWidth, originalHeight);
164+
var bitmapResult = decodeSampledBitmapFromFile(storagePath, originalWidth, originalHeight);
165+
if (bitmapResult == null) {
166+
return null;
167+
}
139168

140169
// Calculate the scaling factors based on screen dimensions
141170
var widthScaleFactor = (float) minWidth/ bitmapResult.getWidth();

0 commit comments

Comments
 (0)