diff --git a/android/src/main/java/com/reactlibrary/RNImgToBase64Module.java b/android/src/main/java/com/reactlibrary/RNImgToBase64Module.java index 7a7b8b7..c049162 100644 --- a/android/src/main/java/com/reactlibrary/RNImgToBase64Module.java +++ b/android/src/main/java/com/reactlibrary/RNImgToBase64Module.java @@ -12,7 +12,7 @@ import com.facebook.react.bridge.ReactApplicationContext; import com.facebook.react.bridge.ReactContextBaseJavaModule; import com.facebook.react.bridge.ReactMethod; -import com.facebook.react.bridge.Callback; +import com.facebook.react.bridge.Promise; public class RNImgToBase64Module extends ReactContextBaseJavaModule { @@ -29,13 +29,13 @@ public String getName() { } @ReactMethod - public void getBase64String(String uri, Callback callback) { + public void getBase64String(String uri, Promise promise) { try { Bitmap image = MediaStore.Images.Media.getBitmap(reactContext.getContentResolver(), Uri.parse(uri)); if (image == null) { - callback.invoke("Failed to decode Bitmap, uri: " + uri); + promise.reject("Error", "Failed to decode Bitmap, uri: " + uri); } else { - callback.invoke(null, bitmapToBase64(image)); + promise.resolve(bitmapToBase64(image)); } } catch (Error e) { e.printStackTrace(); diff --git a/ios/RNImgToBase64.m b/ios/RNImgToBase64.m index 360c1e6..b286173 100644 --- a/ios/RNImgToBase64.m +++ b/ios/RNImgToBase64.m @@ -10,11 +10,14 @@ - (dispatch_queue_t)methodQueue RCT_EXPORT_MODULE() #pragma mark getBase64String -RCT_EXPORT_METHOD(getBase64String:(NSURL*)url callback:(RCTResponseSenderBlock)callback) { +RCT_EXPORT_METHOD(getBase64String:(NSURL*)url + resolve:(RCTPromiseResolveBlock)resolve + rejecter:(RCTPromiseRejectBlock)reject +) { dispatch_async(dispatch_queue_create("image_processing", 0), ^{ NSData* data = [NSData dataWithContentsOfURL:url]; dispatch_async(dispatch_get_main_queue(), ^{ - callback(@[[NSNull null], [data base64EncodedStringWithOptions:0]]); + resolve([data base64EncodedStringWithOptions:0]); }); }); } diff --git a/package.json b/package.json index b73ef48..3c6de75 100644 --- a/package.json +++ b/package.json @@ -1,7 +1,7 @@ { "name": "react-native-image-base64", - "version": "0.1.1", + "version": "0.1.2", "description": "Convert image to base64", "main": "index.js", "scripts": {