| 
				
			 | 
			
			
				@@ -3,15 +3,18 @@ package fr.greweb.reactnativeviewshot; 
			 | 
		
	
		
			
			| 
				3
			 | 
			
				3
			 | 
			
			
				 import javax.annotation.Nullable; 
			 | 
		
	
		
			
			| 
				4
			 | 
			
				4
			 | 
			
			
				 import android.graphics.Bitmap; 
			 | 
		
	
		
			
			| 
				5
			 | 
			
				5
			 | 
			
			
				 import android.net.Uri; 
			 | 
		
	
		
			
			| 
				
			 | 
			
				6
			 | 
			
			
				+import android.util.Base64; 
			 | 
		
	
		
			
			| 
				6
			 | 
			
				7
			 | 
			
			
				 import android.view.View; 
			 | 
		
	
		
			
			| 
				7
			 | 
			
				8
			 | 
			
			
				  
			 | 
		
	
		
			
			| 
				8
			 | 
			
				9
			 | 
			
			
				 import com.facebook.react.bridge.Promise; 
			 | 
		
	
		
			
			| 
				9
			 | 
			
				10
			 | 
			
			
				 import com.facebook.react.uimanager.NativeViewHierarchyManager; 
			 | 
		
	
		
			
			| 
				10
			 | 
			
				11
			 | 
			
			
				 import com.facebook.react.uimanager.UIBlock; 
			 | 
		
	
		
			
			| 
				11
			 | 
			
				12
			 | 
			
			
				  
			 | 
		
	
		
			
			| 
				
			 | 
			
				13
			 | 
			
			
				+import java.io.ByteArrayOutputStream; 
			 | 
		
	
		
			
			| 
				12
			 | 
			
				14
			 | 
			
			
				 import java.io.File; 
			 | 
		
	
		
			
			| 
				13
			 | 
			
				15
			 | 
			
			
				 import java.io.FileOutputStream; 
			 | 
		
	
		
			
			| 
				14
			 | 
			
				16
			 | 
			
			
				 import java.io.IOException; 
			 | 
		
	
		
			
			| 
				
			 | 
			
				17
			 | 
			
			
				+import java.io.OutputStream; 
			 | 
		
	
		
			
			| 
				15
			 | 
			
				18
			 | 
			
			
				  
			 | 
		
	
		
			
			| 
				16
			 | 
			
				19
			 | 
			
			
				 /** 
			 | 
		
	
		
			
			| 
				17
			 | 
			
				20
			 | 
			
			
				  * Snapshot utility class allow to screenshot a view. 
			 | 
		
	
	
		
			
			| 
				
			 | 
			
			
				@@ -26,6 +29,7 @@ public class ViewShot implements UIBlock { 
			 | 
		
	
		
			
			| 
				26
			 | 
			
				29
			 | 
			
			
				     private Integer width; 
			 | 
		
	
		
			
			| 
				27
			 | 
			
				30
			 | 
			
			
				     private Integer height; 
			 | 
		
	
		
			
			| 
				28
			 | 
			
				31
			 | 
			
			
				     private File output; 
			 | 
		
	
		
			
			| 
				
			 | 
			
				32
			 | 
			
			
				+    private boolean base64; 
			 | 
		
	
		
			
			| 
				29
			 | 
			
				33
			 | 
			
			
				     private Promise promise; 
			 | 
		
	
		
			
			| 
				30
			 | 
			
				34
			 | 
			
			
				  
			 | 
		
	
		
			
			| 
				31
			 | 
			
				35
			 | 
			
			
				     public ViewShot( 
			 | 
		
	
	
		
			
			| 
				
			 | 
			
			
				@@ -35,6 +39,7 @@ public class ViewShot implements UIBlock { 
			 | 
		
	
		
			
			| 
				35
			 | 
			
				39
			 | 
			
			
				             @Nullable Integer width, 
			 | 
		
	
		
			
			| 
				36
			 | 
			
				40
			 | 
			
			
				             @Nullable Integer height, 
			 | 
		
	
		
			
			| 
				37
			 | 
			
				41
			 | 
			
			
				             File output, 
			 | 
		
	
		
			
			| 
				
			 | 
			
				42
			 | 
			
			
				+            boolean base64, 
			 | 
		
	
		
			
			| 
				38
			 | 
			
				43
			 | 
			
			
				             Promise promise) { 
			 | 
		
	
		
			
			| 
				39
			 | 
			
				44
			 | 
			
			
				         this.tag = tag; 
			 | 
		
	
		
			
			| 
				40
			 | 
			
				45
			 | 
			
			
				         this.format = format; 
			 | 
		
	
	
		
			
			| 
				
			 | 
			
			
				@@ -42,26 +47,35 @@ public class ViewShot implements UIBlock { 
			 | 
		
	
		
			
			| 
				42
			 | 
			
				47
			 | 
			
			
				         this.width = width; 
			 | 
		
	
		
			
			| 
				43
			 | 
			
				48
			 | 
			
			
				         this.height = height; 
			 | 
		
	
		
			
			| 
				44
			 | 
			
				49
			 | 
			
			
				         this.output = output; 
			 | 
		
	
		
			
			| 
				
			 | 
			
				50
			 | 
			
			
				+        this.base64 = base64; 
			 | 
		
	
		
			
			| 
				45
			 | 
			
				51
			 | 
			
			
				         this.promise = promise; 
			 | 
		
	
		
			
			| 
				46
			 | 
			
				52
			 | 
			
			
				     } 
			 | 
		
	
		
			
			| 
				47
			 | 
			
				53
			 | 
			
			
				  
			 | 
		
	
		
			
			| 
				48
			 | 
			
				54
			 | 
			
			
				     @Override 
			 | 
		
	
		
			
			| 
				49
			 | 
			
				55
			 | 
			
			
				     public void execute(NativeViewHierarchyManager nativeViewHierarchyManager) { 
			 | 
		
	
		
			
			| 
				50
			 | 
			
				
			 | 
			
			
				-        FileOutputStream fos = null; 
			 | 
		
	
		
			
			| 
				
			 | 
			
				56
			 | 
			
			
				+        OutputStream os = null; 
			 | 
		
	
		
			
			| 
				51
			 | 
			
				57
			 | 
			
			
				         View view = nativeViewHierarchyManager.resolveView(tag); 
			 | 
		
	
		
			
			| 
				52
			 | 
			
				58
			 | 
			
			
				         try { 
			 | 
		
	
		
			
			| 
				53
			 | 
			
				
			 | 
			
			
				-            fos = new FileOutputStream(output); 
			 | 
		
	
		
			
			| 
				54
			 | 
			
				
			 | 
			
			
				-            captureView(view, fos); 
			 | 
		
	
		
			
			| 
				55
			 | 
			
				
			 | 
			
			
				-            String uri = Uri.fromFile(output).toString(); 
			 | 
		
	
		
			
			| 
				56
			 | 
			
				
			 | 
			
			
				-            promise.resolve(uri); 
			 | 
		
	
		
			
			| 
				
			 | 
			
				59
			 | 
			
			
				+            if (base64) { 
			 | 
		
	
		
			
			| 
				
			 | 
			
				60
			 | 
			
			
				+                os = new ByteArrayOutputStream(); 
			 | 
		
	
		
			
			| 
				
			 | 
			
				61
			 | 
			
			
				+                captureView(view, os); 
			 | 
		
	
		
			
			| 
				
			 | 
			
				62
			 | 
			
			
				+                byte[] bytes = ((ByteArrayOutputStream) os).toByteArray(); 
			 | 
		
	
		
			
			| 
				
			 | 
			
				63
			 | 
			
			
				+                String data = Base64.encodeToString(bytes, Base64.NO_WRAP); 
			 | 
		
	
		
			
			| 
				
			 | 
			
				64
			 | 
			
			
				+                promise.resolve(data); 
			 | 
		
	
		
			
			| 
				
			 | 
			
				65
			 | 
			
			
				+            } else { 
			 | 
		
	
		
			
			| 
				
			 | 
			
				66
			 | 
			
			
				+                os = new FileOutputStream(output); 
			 | 
		
	
		
			
			| 
				
			 | 
			
				67
			 | 
			
			
				+                captureView(view, os); 
			 | 
		
	
		
			
			| 
				
			 | 
			
				68
			 | 
			
			
				+                String uri = Uri.fromFile(output).toString(); 
			 | 
		
	
		
			
			| 
				
			 | 
			
				69
			 | 
			
			
				+                promise.resolve(uri); 
			 | 
		
	
		
			
			| 
				
			 | 
			
				70
			 | 
			
			
				+            } 
			 | 
		
	
		
			
			| 
				57
			 | 
			
				71
			 | 
			
			
				         } 
			 | 
		
	
		
			
			| 
				58
			 | 
			
				72
			 | 
			
			
				         catch (Exception e) { 
			 | 
		
	
		
			
			| 
				59
			 | 
			
				73
			 | 
			
			
				             promise.reject(ERROR_UNABLE_TO_SNAPSHOT, "Failed to snapshot view tag "+tag); 
			 | 
		
	
		
			
			| 
				60
			 | 
			
				74
			 | 
			
			
				         } 
			 | 
		
	
		
			
			| 
				61
			 | 
			
				75
			 | 
			
			
				         finally { 
			 | 
		
	
		
			
			| 
				62
			 | 
			
				
			 | 
			
			
				-            if (fos != null) { 
			 | 
		
	
		
			
			| 
				
			 | 
			
				76
			 | 
			
			
				+            if (os != null) { 
			 | 
		
	
		
			
			| 
				63
			 | 
			
				77
			 | 
			
			
				                 try { 
			 | 
		
	
		
			
			| 
				64
			 | 
			
				
			 | 
			
			
				-                    fos.close(); 
			 | 
		
	
		
			
			| 
				
			 | 
			
				78
			 | 
			
			
				+                    os.close(); 
			 | 
		
	
		
			
			| 
				65
			 | 
			
				79
			 | 
			
			
				                 } catch (IOException e) { 
			 | 
		
	
		
			
			| 
				66
			 | 
			
				80
			 | 
			
			
				                     e.printStackTrace(); 
			 | 
		
	
		
			
			| 
				67
			 | 
			
				81
			 | 
			
			
				                 } 
			 | 
		
	
	
		
			
			| 
				
			 | 
			
			
				@@ -74,7 +88,7 @@ public class ViewShot implements UIBlock { 
			 | 
		
	
		
			
			| 
				74
			 | 
			
				88
			 | 
			
			
				      * @param view the view to capture 
			 | 
		
	
		
			
			| 
				75
			 | 
			
				89
			 | 
			
			
				      * @return the screenshot or null if it failed. 
			 | 
		
	
		
			
			| 
				76
			 | 
			
				90
			 | 
			
			
				      */ 
			 | 
		
	
		
			
			| 
				77
			 | 
			
				
			 | 
			
			
				-    private void captureView (View view, FileOutputStream fos) { 
			 | 
		
	
		
			
			| 
				
			 | 
			
				91
			 | 
			
			
				+    private void captureView (View view, OutputStream os) { 
			 | 
		
	
		
			
			| 
				78
			 | 
			
				92
			 | 
			
			
				         int w = view.getWidth(); 
			 | 
		
	
		
			
			| 
				79
			 | 
			
				93
			 | 
			
			
				         int h = view.getHeight(); 
			 | 
		
	
		
			
			| 
				80
			 | 
			
				94
			 | 
			
			
				         if (w <= 0 || h <= 0) { 
			 | 
		
	
	
		
			
			| 
				
			 | 
			
			
				@@ -90,6 +104,6 @@ public class ViewShot implements UIBlock { 
			 | 
		
	
		
			
			| 
				90
			 | 
			
				104
			 | 
			
			
				         if (bitmap == null) { 
			 | 
		
	
		
			
			| 
				91
			 | 
			
				105
			 | 
			
			
				             throw new RuntimeException("Impossible to snapshot the view"); 
			 | 
		
	
		
			
			| 
				92
			 | 
			
				106
			 | 
			
			
				         } 
			 | 
		
	
		
			
			| 
				93
			 | 
			
				
			 | 
			
			
				-        bitmap.compress(format, (int)(100.0 * quality), fos); 
			 | 
		
	
		
			
			| 
				
			 | 
			
				107
			 | 
			
			
				+        bitmap.compress(format, (int)(100.0 * quality), os); 
			 | 
		
	
		
			
			| 
				94
			 | 
			
				108
			 | 
			
			
				     } 
			 | 
		
	
		
			
			| 
				95
			 | 
			
				
			 | 
			
			
				-} 
			 | 
		
	
		
			
			| 
				
			 | 
			
				109
			 | 
			
			
				+} 
			 |