Przeglądaj źródła

Merge pull request #169 from jgreen210/android_close_stream_before_resolve_promise

close java OutputStreams before resolve Promises
Gaëtan Renaudeau 5 lat temu
rodzic
commit
b561328af5
No account linked to committer's email address

+ 32
- 38
android/src/main/java/fr/greweb/reactnativeviewshot/ViewShot.java Wyświetl plik

@@ -33,18 +33,18 @@ public class ViewShot implements UIBlock {
33 33
 
34 34
     static final String ERROR_UNABLE_TO_SNAPSHOT = "E_UNABLE_TO_SNAPSHOT";
35 35
 
36
-    private int tag;
37
-    private String extension;
38
-    private Bitmap.CompressFormat format;
39
-    private double quality;
40
-    private Integer width;
41
-    private Integer height;
42
-    private File output;
43
-    private String result;
44
-    private Promise promise;
45
-    private Boolean snapshotContentContainer;
46
-    private  ReactApplicationContext reactContext;
47
-    private Activity currentActivity;
36
+    private final int tag;
37
+    private final String extension;
38
+    private final Bitmap.CompressFormat format;
39
+    private final double quality;
40
+    private final Integer width;
41
+    private final Integer height;
42
+    private final File output;
43
+    private final String result;
44
+    private final Promise promise;
45
+    private final Boolean snapshotContentContainer;
46
+    private final ReactApplicationContext reactContext;
47
+    private final Activity currentActivity;
48 48
 
49 49
     public ViewShot(
50 50
             int tag,
@@ -75,8 +75,7 @@ public class ViewShot implements UIBlock {
75 75
 
76 76
     @Override
77 77
     public void execute(NativeViewHierarchyManager nativeViewHierarchyManager) {
78
-        OutputStream os = null;
79
-        View view = null;
78
+        final View view;
80 79
 
81 80
         if (tag == -1) {
82 81
             view = currentActivity.getWindow().getDecorView().findViewById(android.R.id.content);
@@ -90,22 +89,19 @@ public class ViewShot implements UIBlock {
90 89
         }
91 90
         try {
92 91
             if ("tmpfile".equals(result)) {
93
-                os = new FileOutputStream(output);
94
-                captureView(view, os);
95
-                String uri = Uri.fromFile(output).toString();
92
+                captureView(view, new FileOutputStream(output));
93
+                final String uri = Uri.fromFile(output).toString();
96 94
                 promise.resolve(uri);
97
-            }
98
-            else if ("base64".equals(result)) {
99
-                os = new ByteArrayOutputStream();
95
+            } else if ("base64".equals(result)) {
96
+                final ByteArrayOutputStream os = new ByteArrayOutputStream();
100 97
                 captureView(view, os);
101
-                byte[] bytes = ((ByteArrayOutputStream) os).toByteArray();
102
-                String data = Base64.encodeToString(bytes, Base64.NO_WRAP);
98
+                final byte[] bytes = os.toByteArray();
99
+                final String data = Base64.encodeToString(bytes, Base64.NO_WRAP);
103 100
                 promise.resolve(data);
104
-            }
105
-            else if ("data-uri".equals(result)) {
106
-                os = new ByteArrayOutputStream();
101
+            } else if ("data-uri".equals(result)) {
102
+                final ByteArrayOutputStream os = new ByteArrayOutputStream();
107 103
                 captureView(view, os);
108
-                byte[] bytes = ((ByteArrayOutputStream) os).toByteArray();
104
+                final byte[] bytes = os.toByteArray();
109 105
                 String data = Base64.encodeToString(bytes, Base64.NO_WRAP);
110 106
                 // correct the extension if JPG
111 107
                 if ("jpg".equals(extension)) {
@@ -114,20 +110,10 @@ public class ViewShot implements UIBlock {
114 110
                 data = "data:image/"+extension+";base64," + data;
115 111
                 promise.resolve(data);
116 112
             }
117
-        }
118
-        catch (Exception e) {
113
+        } catch (Exception e) {
119 114
             e.printStackTrace();
120 115
             promise.reject(ERROR_UNABLE_TO_SNAPSHOT, "Failed to capture view snapshot");
121 116
         }
122
-        finally {
123
-            if (os != null) {
124
-                try {
125
-                    os.close();
126
-                } catch (IOException e) {
127
-                    e.printStackTrace();
128
-                }
129
-            }
130
-        }
131 117
     }
132 118
 
133 119
     private List<View> getAllChildren(View v) {
@@ -156,7 +142,15 @@ public class ViewShot implements UIBlock {
156 142
      * @param view the view to capture
157 143
      * @return the screenshot or null if it failed.
158 144
      */
159
-    private void captureView (View view, OutputStream os) {
145
+    private void captureView(View view, OutputStream os) throws IOException {
146
+        try {
147
+            captureViewImpl(view, os);
148
+        } finally {
149
+            os.close();
150
+        }
151
+    }
152
+
153
+    private void captureViewImpl(View view, OutputStream os) {
160 154
         int w = view.getWidth();
161 155
         int h = view.getHeight();
162 156