Skip to content

Commit

Permalink
add custom file name props for android (#318)
Browse files Browse the repository at this point in the history
Co-authored-by: khaiyuen <khaiyuen.lau@zchwantech.com>
  • Loading branch information
LkyYuen and lkyyuenzt authored May 28, 2022
1 parent d1751f1 commit a7eae22
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 3 deletions.
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ class ExampleCaptureOnMountManually extends Component {
}
render() {
return (
<ViewShot ref="viewShot" options={{ format: "jpg", quality: 0.9 }}>
<ViewShot ref="viewShot" options={{ fileName: "Your-File-Name" format: "jpg", quality: 0.9 }}>
<Text>...Something to rasterize...</Text>
</ViewShot>
);
Expand Down Expand Up @@ -147,6 +147,7 @@ Returns a Promise of the image URI.

- **`view`** is a reference to a React Native component.
- **`options`** may include:
- **`fileName`** _(string)_: the file name of the file.
- **`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).
- **`format`** _(string)_: either `png` or `jpg` or `webm` (Android). Defaults to `png`.
- **`quality`** _(number)_: the quality. 0.0 - 1.0 (default). (only available on lossy formats like jpg)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -84,12 +84,13 @@ public void captureRef(int tag, ReadableMap options, Promise promise) {
final Integer scaleWidth = options.hasKey("width") ? (int) (dm.density * options.getDouble("width")) : null;
final Integer scaleHeight = options.hasKey("height") ? (int) (dm.density * options.getDouble("height")) : null;
final String resultStreamFormat = options.getString("result");
final String fileName = options.hasKey("fileName") ? options.getString("fileName") : null;
final Boolean snapshotContentContainer = options.getBoolean("snapshotContentContainer");

try {
File outputFile = null;
if (Results.TEMP_FILE.equals(resultStreamFormat)) {
outputFile = createTempFile(getReactApplicationContext(), extension);
outputFile = createTempFile(getReactApplicationContext(), extension, fileName);
}

final Activity activity = getCurrentActivity();
Expand Down Expand Up @@ -163,7 +164,7 @@ private void cleanDirectory(@NonNull final File directory) {
* whichever is available and has more free space.
*/
@NonNull
private File createTempFile(@NonNull final Context context, @NonNull final String ext) throws IOException {
private File createTempFile(@NonNull final Context context, @NonNull final String ext, String fileName) throws IOException {
final File externalCacheDir = context.getExternalCacheDir();
final File internalCacheDir = context.getCacheDir();
final File cacheDir;
Expand All @@ -182,6 +183,9 @@ private File createTempFile(@NonNull final Context context, @NonNull final Strin
}

final String suffix = "." + ext;
if (fileName != null) {
return File.createTempFile(fileName, suffix, cacheDir);
}
return File.createTempFile(TEMP_FILE_PREFIX, suffix, cacheDir);
}
}

0 comments on commit a7eae22

Please sign in to comment.