reslove #7
This commit is contained in:
parent
bc08c23a54
commit
cdde0a4d0a
|
@ -1,14 +1,17 @@
|
||||||
package fr.bamlab.rnimageresizer;
|
package fr.bamlab.rnimageresizer;
|
||||||
|
|
||||||
import android.content.Context;
|
import android.content.Context;
|
||||||
|
import android.content.ContentResolver;
|
||||||
import android.graphics.Bitmap;
|
import android.graphics.Bitmap;
|
||||||
import android.graphics.BitmapFactory;
|
import android.graphics.BitmapFactory;
|
||||||
import android.graphics.Matrix;
|
import android.graphics.Matrix;
|
||||||
import android.media.ThumbnailUtils;
|
import android.media.ThumbnailUtils;
|
||||||
|
import android.net.Uri;
|
||||||
|
|
||||||
import java.io.ByteArrayOutputStream;
|
import java.io.ByteArrayOutputStream;
|
||||||
import java.io.File;
|
import java.io.File;
|
||||||
import java.io.FileOutputStream;
|
import java.io.FileOutputStream;
|
||||||
|
import java.io.InputStream;
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.util.Date;
|
import java.util.Date;
|
||||||
|
|
||||||
|
@ -17,9 +20,19 @@ import java.util.Date;
|
||||||
*/
|
*/
|
||||||
class ImageResizer {
|
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 {
|
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) {
|
if (image == null) {
|
||||||
return null; // Can't load the image from the given path.
|
return null; // Can't load the image from the given path.
|
||||||
}
|
}
|
||||||
|
@ -41,7 +54,9 @@ class ImageResizer {
|
||||||
}
|
}
|
||||||
|
|
||||||
return image;
|
return image;
|
||||||
} catch (OutOfMemoryError ex) {
|
}catch (IOException ex) {
|
||||||
|
// No memory available for resizing.
|
||||||
|
}catch (OutOfMemoryError ex) {
|
||||||
// No memory available for resizing.
|
// No memory available for resizing.
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -97,9 +112,9 @@ class ImageResizer {
|
||||||
|
|
||||||
public static String createResizedImage(Context context, String imagePath, int newWidth,
|
public static String createResizedImage(Context context, String imagePath, int newWidth,
|
||||||
int newHeight, Bitmap.CompressFormat compressFormat,
|
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(),
|
return ImageResizer.saveImage(resizedImage, context.getCacheDir(),
|
||||||
Long.toString(new Date().getTime()), compressFormat, quality);
|
Long.toString(new Date().getTime()), compressFormat, quality);
|
||||||
}
|
}
|
||||||
|
|
|
@ -44,7 +44,13 @@ RCT_EXPORT_METHOD(createResizedImage:(NSString *)path
|
||||||
|
|
||||||
[_bridge.imageLoader loadImageWithTag:path callback:^(NSError *error, UIImage *image) {
|
[_bridge.imageLoader loadImageWithTag:path callback:^(NSError *error, UIImage *image) {
|
||||||
if (error || image == nil) {
|
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];
|
image = [[UIImage alloc] initWithContentsOfFile:path];
|
||||||
|
}
|
||||||
if (image == nil) {
|
if (image == nil) {
|
||||||
callback(@[@"Can't retrieve the file from the path.", @""]);
|
callback(@[@"Can't retrieve the file from the path.", @""]);
|
||||||
return;
|
return;
|
||||||
|
|
Loading…
Reference in New Issue