35
35
import com .owncloud .android .lib .resources .users .StatusType ;
36
36
import com .owncloud .android .ui .StatusDrawable ;
37
37
38
-
39
38
import java .io .File ;
39
+ import java .io .IOException ;
40
40
import java .nio .charset .StandardCharsets ;
41
41
import java .security .MessageDigest ;
42
42
import java .security .NoSuchAlgorithmException ;
43
43
import java .util .Locale ;
44
44
45
45
import androidx .annotation .NonNull ;
46
46
import androidx .annotation .Nullable ;
47
+ import androidx .annotation .RequiresApi ;
47
48
import androidx .core .graphics .drawable .RoundedBitmapDrawable ;
48
49
import androidx .core .graphics .drawable .RoundedBitmapDrawableFactory ;
49
50
import androidx .exifinterface .media .ExifInterface ;
@@ -75,6 +76,28 @@ public static Bitmap addColorFilter(Bitmap originalBitmap, int filterColor, int
75
76
return resultBitmap ;
76
77
}
77
78
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
+
78
101
/**
79
102
* Decodes a bitmap from a file containing it minimizing the memory use, known that the bitmap will be drawn in a
80
103
* surface of reqWidth x reqHeight
@@ -84,19 +107,23 @@ public static Bitmap addColorFilter(Bitmap originalBitmap, int filterColor, int
84
107
* @param reqHeight Height of the surface where the Bitmap will be drawn on, in pixels.
85
108
* @return decoded bitmap
86
109
*/
110
+ @ Nullable
87
111
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
+
88
118
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 ;
98
122
}
99
123
}
124
+
125
+ Log_OC .i (TAG , "Decoding Bitmap via BitmapFactory.decodeFile" );
126
+
100
127
// set desired options that will affect the size of the bitmap
101
128
final Options options = new Options ();
102
129
@@ -134,8 +161,10 @@ public static Bitmap retrieveBitmapFromFile(String storagePath, int minWidth, in
134
161
originalHeight = tempWidth ;
135
162
}
136
163
137
- var bitmapResult = decodeSampledBitmapFromFile (
138
- storagePath , originalWidth , originalHeight );
164
+ var bitmapResult = decodeSampledBitmapFromFile (storagePath , originalWidth , originalHeight );
165
+ if (bitmapResult == null ) {
166
+ return null ;
167
+ }
139
168
140
169
// Calculate the scaling factors based on screen dimensions
141
170
var widthScaleFactor = (float ) minWidth / bitmapResult .getWidth ();
0 commit comments