Browse Source

Merge pull request #9 from underscopeio/master

Add custom filename support (Android only)
Gaëtan Renaudeau 8 years ago
parent
commit
f3fe488bc3
3 changed files with 13 additions and 3 deletions
  1. 1
    0
      README.md
  2. 11
    3
      android/src/main/java/fr/greweb/reactnativeviewshot/RNViewShotModule.java
  3. 1
    0
      index.js

+ 1
- 0
README.md View File

@@ -39,6 +39,7 @@ Returns a Promise of the image URI.
39 39
     - `"file"` (default): save to a temporary file *(that will only exist for as long as the app is running)*.
40 40
     - `"base64"`: encode as base64 and returns the raw string. Use only with small images as this may result of lags (the string is sent over the bridge). *N.B. This is not a data uri, use `data-uri` instead*.
41 41
     - `"data-uri"`: same as `base64` but also includes the [Data URI scheme](https://en.wikipedia.org/wiki/Data_URI_scheme) header.
42
+ - **`filename`** *(string)*: the name of the generated file if any (Android only). Defaults to `ReactNative_snapshot_image_${timestamp}`.
42 43
 
43 44
 ## Notes
44 45
 

+ 11
- 3
android/src/main/java/fr/greweb/reactnativeviewshot/RNViewShotModule.java View File

@@ -65,7 +65,8 @@ public class RNViewShotModule extends ReactContextBaseJavaModule {
65 65
         Integer height = options.hasKey("height") ? (int)(displayMetrics.density * options.getDouble("height")) : null;
66 66
         String result = options.hasKey("result") ? options.getString("result") : "file";
67 67
         try {
68
-            File tmpFile = "file".equals(result) ? createTempFile(getReactApplicationContext(), format) : null;
68
+            String name = options.hasKey("filename") ? options.getString("filename") : null;
69
+            File tmpFile = "file".equals(result) ? createTempFile(getReactApplicationContext(), format, name) : null;
69 70
             UIManagerModule uiManager = this.reactContext.getNativeModule(UIManagerModule.class);
70 71
             uiManager.addUIBlock(new ViewShot(tag, format, compressFormat, quality, width, height, tmpFile, result, promise));
71 72
         }
@@ -118,7 +119,7 @@ public class RNViewShotModule extends ReactContextBaseJavaModule {
118 119
      * Create a temporary file in the cache directory on either internal or external storage,
119 120
      * whichever is available and has more free space.
120 121
      */
121
-    private File createTempFile(Context context, String ext)
122
+    private File createTempFile(Context context, String ext, String name)
122 123
             throws IOException {
123 124
         File externalCacheDir = context.getExternalCacheDir();
124 125
         File internalCacheDir = context.getCacheDir();
@@ -136,7 +137,14 @@ public class RNViewShotModule extends ReactContextBaseJavaModule {
136 137
                     externalCacheDir : internalCacheDir;
137 138
         }
138 139
         String suffix = "." + ext;
139
-        return File.createTempFile(TEMP_FILE_PREFIX, suffix, cacheDir);
140
+        File tmpFile = File.createTempFile(TEMP_FILE_PREFIX, suffix, cacheDir);
141
+        if (name != null) {
142
+            File renamed = new File(cacheDir, name + suffix);
143
+            tmpFile.renameTo(renamed);
144
+            return renamed;
145
+        }
146
+
147
+        return tmpFile;
140 148
     }
141 149
 
142 150
 }

+ 1
- 0
index.js View File

@@ -12,6 +12,7 @@ export function takeSnapshot(
12 12
     format ?: "png" | "jpg" | "jpeg" | "webm";
13 13
     quality ?: number;
14 14
     base64 ?: bool;
15
+    filename ?: string;
15 16
   }
16 17
 ): Promise<string> {
17 18
   if (typeof view !== "number") {