This commit is contained in:
timzaak 2016-05-11 22:34:42 +08:00
parent bc08c23a54
commit cdde0a4d0a
2 changed files with 27 additions and 6 deletions

View File

@ -1,14 +1,17 @@
package fr.bamlab.rnimageresizer;
import android.content.Context;
import android.content.ContentResolver;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Matrix;
import android.media.ThumbnailUtils;
import android.net.Uri;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.IOException;
import java.util.Date;
@ -17,9 +20,19 @@ import java.util.Date;
*/
class ImageResizer {
private static Bitmap resizeImage(String imagePath, int maxWidth, int maxHeight) {
private static Bitmap resizeImage(String imagePath, int maxWidth, int maxHeight, Context context) {
try {
Bitmap image = BitmapFactory.decodeFile(imagePath);
Bitmap image;
if (!imagePath.startsWith("content://") && !imagePath.startsWith("file://")) {
image = BitmapFactory.decodeFile(imagePath);
} else {
ContentResolver cr = context.getContentResolver();
Uri url = Uri.parse(imagePath);
InputStream input = cr.openInputStream(url);
image = BitmapFactory.decodeStream(input);
input.close();
}
if (image == null) {
return null; // Can't load the image from the given path.
}
@ -41,7 +54,9 @@ class ImageResizer {
}
return image;
} catch (OutOfMemoryError ex) {
}catch (IOException ex) {
// No memory available for resizing.
}catch (OutOfMemoryError ex) {
// No memory available for resizing.
}
@ -97,9 +112,9 @@ class ImageResizer {
public static String createResizedImage(Context context, String imagePath, int newWidth,
int newHeight, Bitmap.CompressFormat compressFormat,
int quality, int rotation) throws IOException {
int quality, int rotation) {
Bitmap resizedImage = ImageResizer.rotateImage(ImageResizer.resizeImage(imagePath, newWidth, newHeight), rotation);
Bitmap resizedImage = ImageResizer.rotateImage(ImageResizer.resizeImage(imagePath, newWidth, newHeight,context), rotation);
return ImageResizer.saveImage(resizedImage, context.getCacheDir(),
Long.toString(new Date().getTime()), compressFormat, quality);
}

View File

@ -44,7 +44,13 @@ RCT_EXPORT_METHOD(createResizedImage:(NSString *)path
[_bridge.imageLoader loadImageWithTag:path callback:^(NSError *error, UIImage *image) {
if (error || image == nil) {
UIImage* image;
if ([path hasPrefix:@"data:"] || [path hasPrefix:@"file:"]) {
NSURL *imageUrl = [[NSURL alloc] initWithString:path];
image = [UIImage imageWithData:[NSData dataWithContentsOfURL:imageUrl]];
} else {
image = [[UIImage alloc] initWithContentsOfFile:path];
}
if (image == nil) {
callback(@[@"Can't retrieve the file from the path.", @""]);
return;