rive-react-native icon indicating copy to clipboard operation
rive-react-native copied to clipboard

Being able to import local rive files

Open arasrezaei opened this issue 2 years ago • 3 comments

Hi there, thanks for updating the run times! is there any way we can require('../assets/myRive.riv') rive files so that the run time can consume it? thanks.

arasrezaei avatar May 03 '23 23:05 arasrezaei

Hello, You can follow this https://github.com/rive-app/rive-react-native/issues/123#issuecomment-1522169006, it worked for me.

AChevallier avatar May 11 '23 09:05 AChevallier

Hello, You can follow this #123 (comment), it worked for me.

Thanks, I'm using expo and android, can not apply the patch :(

arasrezaei avatar May 11 '23 10:05 arasrezaei

I am using expo as well and using patch-package and this patch (almost the same patch by nderscore, thanks to him)

diff --git a/node_modules/rive-react-native/android/src/main/java/com/rivereactnative/RiveReactNativeView.kt b/node_modules/rive-react-native/android/src/main/java/com/rivereactnative/RiveReactNativeView.kt
index 53779aa..9996319 100644
--- a/node_modules/rive-react-native/android/src/main/java/com/rivereactnative/RiveReactNativeView.kt
+++ b/node_modules/rive-react-native/android/src/main/java/com/rivereactnative/RiveReactNativeView.kt
@@ -20,6 +20,7 @@ import com.facebook.react.bridge.ReadableArray
 import com.facebook.react.modules.core.ExceptionsManagerModule
 import com.facebook.react.uimanager.ThemedReactContext
 import java.io.UnsupportedEncodingException
+import java.util.Base64
 import kotlin.IllegalStateException
 
 
@@ -300,6 +301,24 @@ class RiveReactNativeView(private val context: ThemedReactContext) : FrameLayout
 
 
   private fun setUrlRiveResource(url: String, autoplay: Boolean = this.autoplay) {
+    if (url.startsWith("data:")) {
+      val b64data = url.substringAfter(',')
+      val decoder: Base64.Decoder = Base64.getDecoder()
+      val bytes = decoder.decode(b64data)
+
+      riveAnimationView.setRiveBytes(
+        bytes,
+        fit = this.fit,
+        alignment = this.alignment,
+        autoplay = autoplay,
+        stateMachineName = this.stateMachineName,
+        animationName = this.animationName,
+        artboardName = this.artboardName
+      )
+
+      return
+    }
+
     val queue = Volley.newRequestQueue(context)
     val stringRequest = RNRiveFileRequest(url, { bytes ->
       try {

I had to rebuild a version with eas to make it work.

AChevallier avatar May 12 '23 08:05 AChevallier