Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add custom file name props for android #318

Merged
merged 1 commit into from
May 28, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@ -126,6 +126,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);
}
}