Merge pull request #8 from timzaak/supportFileContentUri

Fix #6 and #7
This commit is contained in:
Florian Rival 2016-05-16 12:50:04 +02:00
commit d6c96d6273
3 changed files with 29 additions and 13 deletions

View File

@ -2,7 +2,7 @@ apply plugin: 'com.android.library'
android {
compileSdkVersion 23
buildToolsVersion "23.0.2"
buildToolsVersion "23.0.1"
defaultConfig {
minSdkVersion 16
@ -18,6 +18,5 @@ android {
}
dependencies {
compile 'com.android.support:appcompat-v7:23.1.0'
compile 'com.facebook.react:react-native:0.14.+'
compile 'com.facebook.react:react-native:+'
}

View File

@ -1,14 +1,16 @@
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 +19,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 +53,7 @@ class ImageResizer {
}
return image;
} catch (OutOfMemoryError ex) {
}catch (IOException ex) {
// No memory available for resizing.
}
@ -99,7 +111,7 @@ class ImageResizer {
int newHeight, Bitmap.CompressFormat compressFormat,
int quality, int rotation) throws IOException {
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

@ -40,11 +40,16 @@ RCT_EXPORT_METHOD(createResizedImage:(NSString *)path
callback:(RCTResponseSenderBlock)callback)
{
CGSize newSize = CGSizeMake(width, height);
NSString* fullPath = generateCacheFilePath(@".jpg");
NSString* fullPath = generateCacheFilePath(@"jpg");
[_bridge.imageLoader loadImageWithTag:path callback:^(NSError *error, UIImage *image) {
if (error || image == nil) {
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;