Skip to content

Commit 8df4c24

Browse files
committed
Update Android functionality to provide the "captureScreenshot". This will capture the entire screen and all the views currently displayed. This performs a native screen capture so a ref tag is not required.
1 parent 9bae019 commit 8df4c24

File tree

3 files changed

+30
-2
lines changed

3 files changed

+30
-2
lines changed

android/src/main/java/fr/greweb/reactnativeviewshot/RNViewShotModule.java

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,13 +85,18 @@ public void captureRef(int tag, ReadableMap options, Promise promise) {
8585
file = createTempFile(getReactApplicationContext(), format);
8686
}
8787
UIManagerModule uiManager = this.reactContext.getNativeModule(UIManagerModule.class);
88-
uiManager.addUIBlock(new ViewShot(tag, format, compressFormat, quality, width, height, file, result, snapshotContentContainer,reactContext, promise));
88+
uiManager.addUIBlock(new ViewShot(tag, format, compressFormat, quality, width, height, file, result, snapshotContentContainer,reactContext, getCurrentActivity(), promise));
8989
}
9090
catch (Exception e) {
9191
promise.reject(ViewShot.ERROR_UNABLE_TO_SNAPSHOT, "Failed to snapshot view tag "+tag);
9292
}
9393
}
9494

95+
@ReactMethod
96+
public void captureScreenshot(ReadableMap options, Promise promise) {
97+
captureRef(-1, options, promise);
98+
}
99+
95100
private static final String TEMP_FILE_PREFIX = "ReactNative-snapshot-image";
96101

97102
/**

android/src/main/java/fr/greweb/reactnativeviewshot/ViewShot.java

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ public class ViewShot implements UIBlock {
4444
private Promise promise;
4545
private Boolean snapshotContentContainer;
4646
private ReactApplicationContext reactContext;
47+
private Activity currentActivity;
4748

4849
public ViewShot(
4950
int tag,
@@ -56,6 +57,7 @@ public ViewShot(
5657
String result,
5758
Boolean snapshotContentContainer,
5859
ReactApplicationContext reactContext,
60+
Activity currentActivity,
5961
Promise promise) {
6062
this.tag = tag;
6163
this.extension = extension;
@@ -67,13 +69,21 @@ public ViewShot(
6769
this.result = result;
6870
this.snapshotContentContainer = snapshotContentContainer;
6971
this.reactContext = reactContext;
72+
this.currentActivity = currentActivity;
7073
this.promise = promise;
7174
}
7275

7376
@Override
7477
public void execute(NativeViewHierarchyManager nativeViewHierarchyManager) {
7578
OutputStream os = null;
76-
View view = nativeViewHierarchyManager.resolveView(tag);
79+
View view = null;
80+
81+
if (tag == -1) {
82+
view = currentActivity.getWindow().getDecorView().findViewById(android.R.id.content);
83+
} else {
84+
view = nativeViewHierarchyManager.resolveView(tag);
85+
}
86+
7787
if (view == null) {
7888
promise.reject(ERROR_UNABLE_TO_SNAPSHOT, "No view found with reactTag: "+tag);
7989
return;

src/index.js

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,19 @@ export function releaseCapture(uri: string): void {
114114
}
115115
}
116116

117+
export function captureScreenshot(
118+
optionsObject?: Options
119+
): Promise<string> {
120+
const { options, errors } = validateOptions(optionsObject);
121+
if (__DEV__ && errors.length > 0) {
122+
console.warn(
123+
"react-native-view-shot: bad options:\n" +
124+
errors.map(e => `- ${e}`).join("\n")
125+
);
126+
}
127+
return RNViewShot.captureScreenshot(options);
128+
}
129+
117130
type Props = {
118131
options?: Object,
119132
captureMode?: "mount" | "continuous" | "update",

0 commit comments

Comments
 (0)