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
     - `"file"` (default): save to a temporary file *(that will only exist for as long as the app is running)*.
39
     - `"file"` (default): save to a temporary file *(that will only exist for as long as the app is running)*.
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*.
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
     - `"data-uri"`: same as `base64` but also includes the [Data URI scheme](https://en.wikipedia.org/wiki/Data_URI_scheme) header.
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
 ## Notes
44
 ## Notes
44
 
45
 

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

65
         Integer height = options.hasKey("height") ? (int)(displayMetrics.density * options.getDouble("height")) : null;
65
         Integer height = options.hasKey("height") ? (int)(displayMetrics.density * options.getDouble("height")) : null;
66
         String result = options.hasKey("result") ? options.getString("result") : "file";
66
         String result = options.hasKey("result") ? options.getString("result") : "file";
67
         try {
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
             UIManagerModule uiManager = this.reactContext.getNativeModule(UIManagerModule.class);
70
             UIManagerModule uiManager = this.reactContext.getNativeModule(UIManagerModule.class);
70
             uiManager.addUIBlock(new ViewShot(tag, format, compressFormat, quality, width, height, tmpFile, result, promise));
71
             uiManager.addUIBlock(new ViewShot(tag, format, compressFormat, quality, width, height, tmpFile, result, promise));
71
         }
72
         }
118
      * Create a temporary file in the cache directory on either internal or external storage,
119
      * Create a temporary file in the cache directory on either internal or external storage,
119
      * whichever is available and has more free space.
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
             throws IOException {
123
             throws IOException {
123
         File externalCacheDir = context.getExternalCacheDir();
124
         File externalCacheDir = context.getExternalCacheDir();
124
         File internalCacheDir = context.getCacheDir();
125
         File internalCacheDir = context.getCacheDir();
136
                     externalCacheDir : internalCacheDir;
137
                     externalCacheDir : internalCacheDir;
137
         }
138
         }
138
         String suffix = "." + ext;
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
     format ?: "png" | "jpg" | "jpeg" | "webm";
12
     format ?: "png" | "jpg" | "jpeg" | "webm";
13
     quality ?: number;
13
     quality ?: number;
14
     base64 ?: bool;
14
     base64 ?: bool;
15
+    filename ?: string;
15
   }
16
   }
16
 ): Promise<string> {
17
 ): Promise<string> {
17
   if (typeof view !== "number") {
18
   if (typeof view !== "number") {