-
Notifications
You must be signed in to change notification settings - Fork 373
Expand file tree
/
Copy pathStreamChatReactNativeModule.java
More file actions
98 lines (80 loc) · 3.65 KB
/
StreamChatReactNativeModule.java
File metadata and controls
98 lines (80 loc) · 3.65 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
package com.streamchatreactnative;
import android.annotation.SuppressLint;
import android.graphics.Bitmap;
import android.net.Uri;
import android.os.AsyncTask;
import androidx.annotation.Nullable;
import androidx.annotation.NonNull;
import com.facebook.react.bridge.Arguments;
import com.facebook.react.bridge.GuardedAsyncTask;
import com.facebook.react.bridge.Promise;
import com.facebook.react.bridge.ReactApplicationContext;
import com.facebook.react.bridge.ReadableMap;
import com.facebook.react.bridge.WritableMap;
import com.facebook.react.bridge.ReactMethod;
import java.io.File;
import java.io.IOException;
import java.util.UUID;
public class StreamChatReactNativeModule extends StreamChatReactNativeSpec {
public static final String NAME = "StreamChatReactNative";
StreamChatReactNativeModule(ReactApplicationContext reactContext) {
super(reactContext);
}
@Override
@NonNull
public String getName() {
return NAME;
}
@ReactMethod
public void createResizedImage(String uri, double width, double height, String format, double quality, String mode, boolean onlyScaleDown, Double rotation, @Nullable String outputPath, Promise promise) {
WritableMap options = Arguments.createMap();
options.putString("mode", mode);
options.putBoolean("onlyScaleDown", onlyScaleDown);
// Run in guarded async task to prevent blocking the React bridge
new GuardedAsyncTask<Void, Void>(this.getReactApplicationContext()) {
@Override
protected void doInBackgroundGuarded(Void... params) {
try {
Object response = createResizedImageWithExceptions(uri, (int) width, (int) height, format, (int) quality, rotation.intValue(), outputPath, options);
promise.resolve(response);
}
catch (IOException e) {
promise.reject(e);
}
}
}.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
}
@SuppressLint("LongLogTag")
private Object createResizedImageWithExceptions(String imagePath, int newWidth, int newHeight,
String compressFormatString, int quality, int rotation, String outputPath,
final ReadableMap options) throws IOException {
Bitmap.CompressFormat compressFormat = Bitmap.CompressFormat.valueOf(compressFormatString);
Uri imageUri = Uri.parse(imagePath);
Bitmap scaledImage = StreamChatReactNative.createResizedImage(this.getReactApplicationContext(), imageUri, newWidth, newHeight, quality, rotation,
options.getString("mode"), options.getBoolean("onlyScaleDown"));
if (scaledImage == null) {
throw new IOException("The image failed to be resized; invalid Bitmap result.");
}
// Save the resulting image
File path = this.getReactApplicationContext().getCacheDir();
if (outputPath != null) {
path = new File(outputPath);
}
File resizedImage = StreamChatReactNative.saveImage(scaledImage, path, UUID.randomUUID().toString(), compressFormat, quality);
WritableMap response = Arguments.createMap();
// If resizedImagePath is empty and this wasn't caught earlier, throw.
if (resizedImage.isFile()) {
response.putString("path", resizedImage.getAbsolutePath());
response.putString("uri", Uri.fromFile(resizedImage).toString());
response.putString("name", resizedImage.getName());
response.putDouble("size", resizedImage.length());
response.putDouble("width", scaledImage.getWidth());
response.putDouble("height", scaledImage.getHeight());
} else {
throw new IOException("Error getting resized image path");
}
// Clean up bitmap
scaledImage.recycle();
return response;
}
}