Skip to content

Commit cdde0a4

Browse files
committed
reslove #7
1 parent bc08c23 commit cdde0a4

File tree

2 files changed

+27
-6
lines changed

2 files changed

+27
-6
lines changed

android/src/main/java/fr/bamlab/rnimageresizer/ImageResizer.java

Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,17 @@
11
package fr.bamlab.rnimageresizer;
22

33
import android.content.Context;
4+
import android.content.ContentResolver;
45
import android.graphics.Bitmap;
56
import android.graphics.BitmapFactory;
67
import android.graphics.Matrix;
78
import android.media.ThumbnailUtils;
9+
import android.net.Uri;
810

911
import java.io.ByteArrayOutputStream;
1012
import java.io.File;
1113
import java.io.FileOutputStream;
14+
import java.io.InputStream;
1215
import java.io.IOException;
1316
import java.util.Date;
1417

@@ -17,9 +20,19 @@
1720
*/
1821
class 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
}

ios/RCTImageResizer/RCTImageResizer.m

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,13 @@ void saveImage(NSString * fullPath, UIImage * image, float quality)
4444

4545
[_bridge.imageLoader loadImageWithTag:path callback:^(NSError *error, UIImage *image) {
4646
if (error || image == nil) {
47-
image = [[UIImage alloc] initWithContentsOfFile:path];
47+
UIImage* image;
48+
if ([path hasPrefix:@"data:"] || [path hasPrefix:@"file:"]) {
49+
NSURL *imageUrl = [[NSURL alloc] initWithString:path];
50+
image = [UIImage imageWithData:[NSData dataWithContentsOfURL:imageUrl]];
51+
} else {
52+
image = [[UIImage alloc] initWithContentsOfFile:path];
53+
}
4854
if (image == nil) {
4955
callback(@[@"Can't retrieve the file from the path.", @""]);
5056
return;

0 commit comments

Comments
 (0)