Merge pull request #36 from ivpusic/magrinj-base64-data

Magrinj base64 data
This commit is contained in:
Ivan Pusic 2016-08-05 20:47:46 +02:00 committed by GitHub
commit 715e3cdcac
3 changed files with 73 additions and 13 deletions

View File

@ -11,6 +11,7 @@ import android.net.Uri;
import android.os.Build;
import android.provider.DocumentsContract;
import android.provider.MediaStore;
import android.util.Base64;
import com.facebook.react.bridge.ActivityEventListener;
import com.facebook.react.bridge.Promise;
@ -26,6 +27,12 @@ import com.yalantis.ucrop.UCrop;
import java.io.File;
import java.util.UUID;
import java.io.InputStream;
import java.io.FileNotFoundException;
import java.io.FileInputStream;
import java.io.File;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
/**
* Created by ipusic on 5/16/16.
@ -46,6 +53,7 @@ public class PickerModule extends ReactContextBaseJavaModule implements Activity
private boolean cropping = false;
private boolean multiple = false;
private boolean includeBase64 = false;
private int width = 100;
private int height = 100;
@ -69,6 +77,7 @@ public class PickerModule extends ReactContextBaseJavaModule implements Activity
}
multiple = options.hasKey("multiple") && options.getBoolean("multiple");
includeBase64 = options.hasKey("includeBase64") && options.getBoolean("includeBase64");
width = options.hasKey("width") ? options.getInt("width") : width;
height = options.hasKey("height") ? options.getInt("height") : height;
cropping = options.hasKey("cropping") ? options.getBoolean("cropping") : cropping;
@ -91,12 +100,39 @@ public class PickerModule extends ReactContextBaseJavaModule implements Activity
}
}
private String getBase64StringFromFile(String absoluteFilePath) {
InputStream inputStream;
try {
inputStream = new FileInputStream(new File(absoluteFilePath));
} catch (FileNotFoundException e) {
e.printStackTrace();
return null;
}
byte[] bytes;
byte[] buffer = new byte[8192];
int bytesRead;
ByteArrayOutputStream output = new ByteArrayOutputStream();
try {
while ((bytesRead = inputStream.read(buffer)) != -1) {
output.write(buffer, 0, bytesRead);
}
} catch (IOException e) {
e.printStackTrace();
}
bytes = output.toByteArray();
return Base64.encodeToString(bytes, Base64.NO_WRAP);
}
private WritableMap getImage(Uri uri, boolean resolvePath) {
WritableMap image = new WritableNativeMap();
String path = uri.getPath();
if (resolvePath) {
path = RealPathUtil.getRealPathFromURI(activity, uri);
path = RealPathUtil.getRealPathFromURI(activity, uri);
}
BitmapFactory.Options options = new BitmapFactory.Options();
@ -112,7 +148,11 @@ public class PickerModule extends ReactContextBaseJavaModule implements Activity
image.putInt("width", options.outWidth);
image.putInt("height", options.outHeight);
image.putString("mime", options.outMimeType);
image.putInt("size", (int)fileLen);
image.putInt("size", (int) fileLen);
if (includeBase64) {
image.putString("data", getBase64StringFromFile(path));
}
return image;
}

View File

@ -30,6 +30,21 @@ export default class App extends Component {
};
}
pickSingleBase64(cropit) {
ImagePicker.openPicker({
width: 300,
height: 300,
cropping: cropit,
includeBase64: true
}).then(image => {
console.log('received image', image);
this.setState({
image: {uri: `data:${image.mime};base64,`+ image.data, width: image.width, height: image.height},
images: null
});
}).catch(e => {});
}
pickSingle(cropit) {
ImagePicker.openPicker({
width: 300,
@ -72,6 +87,9 @@ export default class App extends Component {
<TouchableOpacity onPress={() => this.pickSingle(false)} style={styles.button}>
<Text style={styles.text}>Select Single</Text>
</TouchableOpacity>
<TouchableOpacity onPress={() => this.pickSingleBase64(false)} style={styles.button}>
<Text style={styles.text}>Select Single Returning Base64</Text>
</TouchableOpacity>
<TouchableOpacity onPress={() => this.pickSingle(true)} style={styles.button}>
<Text style={styles.text}>Select Single With Cropping</Text>
</TouchableOpacity>

View File

@ -17,13 +17,13 @@
RCT_EXPORT_MODULE();
- (instancetype)init
{
if (self = [super init]) {
self.defaultOptions = @{
@"multiple": @NO,
@"cropping": @NO,
@"includeBase64": @NO,
@"maxFiles": @5,
@"width": @200,
@"height": @200
@ -97,7 +97,8 @@ RCT_EXPORT_METHOD(openPicker:(NSDictionary *)options
@"width": @(asset.pixelWidth),
@"height": @(asset.pixelHeight),
@"mime": @"image/jpeg",
@"size": [NSNumber numberWithUnsignedInteger:data.length]
@"size": [NSNumber numberWithUnsignedInteger:data.length],
@"data": [[self.options objectForKey:@"includeBase64"] boolValue] ? [data base64EncodedStringWithOptions:0] : [NSNull null],
}];
}];
}
@ -146,7 +147,8 @@ RCT_EXPORT_METHOD(openPicker:(NSDictionary *)options
@"width": @(asset.pixelWidth),
@"height": @(asset.pixelHeight),
@"mime": @"image/jpeg",
@"size": [NSNumber numberWithUnsignedInteger:data.length]
@"size": [NSNumber numberWithUnsignedInteger:data.length],
@"data": [[self.options objectForKey:@"includeBase64"] boolValue] ? [data base64EncodedStringWithOptions:0] : [NSNull null],
});
[imagePickerController dismissViewControllerAnimated:YES completion:nil];
}
@ -236,15 +238,15 @@ RCT_EXPORT_METHOD(openPicker:(NSDictionary *)options
return;
}
NSDictionary *image = @{
@"path": filePath,
@"width": @(resizedImage.size.width),
@"height": @(resizedImage.size.height),
@"mime": @"image/jpeg",
@"size": [NSNumber numberWithUnsignedInteger:data.length]
};
self.resolve(@{
@"path": filePath,
@"width": @(resizedImage.size.width),
@"height": @(resizedImage.size.height),
@"mime": @"image/jpeg",
@"size": [NSNumber numberWithUnsignedInteger:data.length],
@"data": [[self.options objectForKey:@"includeBase64"] boolValue] ? [data base64EncodedStringWithOptions:0] : [NSNull null],
});
self.resolve(image);
[controller dismissViewControllerAnimated:YES completion:nil];
}