Просмотр исходного кода

Close android OutputStreams before resolving Promise. Otherwise, buffers might not be flushed soon enough.

Jeremy Green 6 лет назад
Родитель
Сommit
0a4efe543b
1 измененных файлов: 20 добавлений и 26 удалений
  1. 20
    26
      android/src/main/java/fr/greweb/reactnativeviewshot/ViewShot.java

+ 20
- 26
android/src/main/java/fr/greweb/reactnativeviewshot/ViewShot.java Просмотреть файл

@@ -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,40 +89,27 @@ 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
                 data = "data:image/"+extension+";base64," + data;
111 107
                 promise.resolve(data);
112 108
             }
113
-        }
114
-        catch (Exception e) {
109
+        } catch (Exception e) {
115 110
             e.printStackTrace();
116 111
             promise.reject(ERROR_UNABLE_TO_SNAPSHOT, "Failed to capture view snapshot");
117 112
         }
118
-        finally {
119
-            if (os != null) {
120
-                try {
121
-                    os.close();
122
-                } catch (IOException e) {
123
-                    e.printStackTrace();
124
-                }
125
-            }
126
-        }
127 113
     }
128 114
 
129 115
     private List<View> getAllChildren(View v) {
@@ -152,7 +138,15 @@ public class ViewShot implements UIBlock {
152 138
      * @param view the view to capture
153 139
      * @return the screenshot or null if it failed.
154 140
      */
155
-    private void captureView (View view, OutputStream os) {
141
+    private void captureView(View view, OutputStream os) throws IOException {
142
+        try {
143
+            captureViewImpl(view, os);
144
+        } finally {
145
+            os.close();
146
+        }
147
+    }
148
+
149
+    private void captureViewImpl(View view, OutputStream os) {
156 150
         int w = view.getWidth();
157 151
         int h = view.getHeight();
158 152