Skip to content

Commit a7eae22

Browse files
LkyYuenlkyyuenzt
andauthored
add custom file name props for android (#318)
Co-authored-by: khaiyuen <[email protected]>
1 parent d1751f1 commit a7eae22

File tree

2 files changed

+8
-3
lines changed

2 files changed

+8
-3
lines changed

README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ class ExampleCaptureOnMountManually extends Component {
4141
}
4242
render() {
4343
return (
44-
<ViewShot ref="viewShot" options={{ format: "jpg", quality: 0.9 }}>
44+
<ViewShot ref="viewShot" options={{ fileName: "Your-File-Name" format: "jpg", quality: 0.9 }}>
4545
<Text>...Something to rasterize...</Text>
4646
</ViewShot>
4747
);
@@ -147,6 +147,7 @@ Returns a Promise of the image URI.
147147

148148
- **`view`** is a reference to a React Native component.
149149
- **`options`** may include:
150+
- **`fileName`** _(string)_: the file name of the file.
150151
- **`width`** / **`height`** _(number)_: the width and height of the final image (resized from the View bound. don't provide it if you want the original pixel size).
151152
- **`format`** _(string)_: either `png` or `jpg` or `webm` (Android). Defaults to `png`.
152153
- **`quality`** _(number)_: the quality. 0.0 - 1.0 (default). (only available on lossy formats like jpg)

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

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -84,12 +84,13 @@ public void captureRef(int tag, ReadableMap options, Promise promise) {
8484
final Integer scaleWidth = options.hasKey("width") ? (int) (dm.density * options.getDouble("width")) : null;
8585
final Integer scaleHeight = options.hasKey("height") ? (int) (dm.density * options.getDouble("height")) : null;
8686
final String resultStreamFormat = options.getString("result");
87+
final String fileName = options.hasKey("fileName") ? options.getString("fileName") : null;
8788
final Boolean snapshotContentContainer = options.getBoolean("snapshotContentContainer");
8889

8990
try {
9091
File outputFile = null;
9192
if (Results.TEMP_FILE.equals(resultStreamFormat)) {
92-
outputFile = createTempFile(getReactApplicationContext(), extension);
93+
outputFile = createTempFile(getReactApplicationContext(), extension, fileName);
9394
}
9495

9596
final Activity activity = getCurrentActivity();
@@ -163,7 +164,7 @@ private void cleanDirectory(@NonNull final File directory) {
163164
* whichever is available and has more free space.
164165
*/
165166
@NonNull
166-
private File createTempFile(@NonNull final Context context, @NonNull final String ext) throws IOException {
167+
private File createTempFile(@NonNull final Context context, @NonNull final String ext, String fileName) throws IOException {
167168
final File externalCacheDir = context.getExternalCacheDir();
168169
final File internalCacheDir = context.getCacheDir();
169170
final File cacheDir;
@@ -182,6 +183,9 @@ private File createTempFile(@NonNull final Context context, @NonNull final Strin
182183
}
183184

184185
final String suffix = "." + ext;
186+
if (fileName != null) {
187+
return File.createTempFile(fileName, suffix, cacheDir);
188+
}
185189
return File.createTempFile(TEMP_FILE_PREFIX, suffix, cacheDir);
186190
}
187191
}

0 commit comments

Comments
 (0)