1
1
package fr .bamlab .rnimageresizer ;
2
2
3
3
import android .content .Context ;
4
+ import android .content .ContentResolver ;
4
5
import android .graphics .Bitmap ;
5
6
import android .graphics .BitmapFactory ;
6
7
import android .graphics .Matrix ;
7
8
import android .media .ThumbnailUtils ;
9
+ import android .net .Uri ;
8
10
9
11
import java .io .ByteArrayOutputStream ;
10
12
import java .io .File ;
11
13
import java .io .FileOutputStream ;
14
+ import java .io .InputStream ;
12
15
import java .io .IOException ;
13
16
import java .util .Date ;
14
17
17
20
*/
18
21
class ImageResizer {
19
22
20
- private static Bitmap resizeImage (String imagePath , int maxWidth , int maxHeight ) {
23
+ private static Bitmap resizeImage (String imagePath , int maxWidth , int maxHeight , Context context ) {
21
24
try {
22
- Bitmap image = BitmapFactory .decodeFile (imagePath );
25
+ Bitmap image ;
26
+ if (!imagePath .startsWith ("content://" ) && !imagePath .startsWith ("file://" )) {
27
+ image = BitmapFactory .decodeFile (imagePath );
28
+ } else {
29
+ ContentResolver cr = context .getContentResolver ();
30
+ Uri url = Uri .parse (imagePath );
31
+ InputStream input = cr .openInputStream (url );
32
+ image = BitmapFactory .decodeStream (input );
33
+ input .close ();
34
+ }
35
+
23
36
if (image == null ) {
24
37
return null ; // Can't load the image from the given path.
25
38
}
@@ -41,7 +54,9 @@ private static Bitmap resizeImage(String imagePath, int maxWidth, int maxHeight)
41
54
}
42
55
43
56
return image ;
44
- } catch (OutOfMemoryError ex ) {
57
+ }catch (IOException ex ) {
58
+ // No memory available for resizing.
59
+ }catch (OutOfMemoryError ex ) {
45
60
// No memory available for resizing.
46
61
}
47
62
@@ -97,9 +112,9 @@ private static String saveImage(Bitmap bitmap, File saveDirectory, String fileNa
97
112
98
113
public static String createResizedImage (Context context , String imagePath , int newWidth ,
99
114
int newHeight , Bitmap .CompressFormat compressFormat ,
100
- int quality , int rotation ) throws IOException {
115
+ int quality , int rotation ) {
101
116
102
- Bitmap resizedImage = ImageResizer .rotateImage (ImageResizer .resizeImage (imagePath , newWidth , newHeight ), rotation );
117
+ Bitmap resizedImage = ImageResizer .rotateImage (ImageResizer .resizeImage (imagePath , newWidth , newHeight , context ), rotation );
103
118
return ImageResizer .saveImage (resizedImage , context .getCacheDir (),
104
119
Long .toString (new Date ().getTime ()), compressFormat , quality );
105
120
}
0 commit comments