11package fr .bamlab .rnimageresizer ;
22
33import android .content .Context ;
4+ import android .content .ContentResolver ;
45import android .graphics .Bitmap ;
56import android .graphics .BitmapFactory ;
67import android .graphics .Matrix ;
78import android .media .ThumbnailUtils ;
9+ import android .net .Uri ;
810
911import java .io .ByteArrayOutputStream ;
1012import java .io .File ;
1113import java .io .FileOutputStream ;
14+ import java .io .InputStream ;
1215import java .io .IOException ;
1316import java .util .Date ;
1417
1720 */
1821class ImageResizer {
1922
20- private static Bitmap resizeImage (String imagePath , int maxWidth , int maxHeight ) {
23+ private static Bitmap resizeImage (String imagePath , int maxWidth , int maxHeight , Context context ) {
2124 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+
2336 if (image == null ) {
2437 return null ; // Can't load the image from the given path.
2538 }
@@ -41,7 +54,9 @@ private static Bitmap resizeImage(String imagePath, int maxWidth, int maxHeight)
4154 }
4255
4356 return image ;
44- } catch (OutOfMemoryError ex ) {
57+ }catch (IOException ex ) {
58+ // No memory available for resizing.
59+ }catch (OutOfMemoryError ex ) {
4560 // No memory available for resizing.
4661 }
4762
@@ -97,9 +112,9 @@ private static String saveImage(Bitmap bitmap, File saveDirectory, String fileNa
97112
98113 public static String createResizedImage (Context context , String imagePath , int newWidth ,
99114 int newHeight , Bitmap .CompressFormat compressFormat ,
100- int quality , int rotation ) throws IOException {
115+ int quality , int rotation ) {
101116
102- Bitmap resizedImage = ImageResizer .rotateImage (ImageResizer .resizeImage (imagePath , newWidth , newHeight ), rotation );
117+ Bitmap resizedImage = ImageResizer .rotateImage (ImageResizer .resizeImage (imagePath , newWidth , newHeight , context ), rotation );
103118 return ImageResizer .saveImage (resizedImage , context .getCacheDir (),
104119 Long .toString (new Date ().getTime ()), compressFormat , quality );
105120 }
0 commit comments