[V2] Android app crash - custom backButton FileNotFoundException
Issue Description
When deploying the app to HockeyApp we're getting an app crash:
java.lang.RuntimeException: java.io.FileNotFoundException: No content provider: assets_images_androidback
at com.reactnativenavigation.viewcontrollers.button.NavigationIconResolver$1.onError(NavigationIconResolver.java:36)
at com.reactnativenavigation.utils.ImageLoader.loadIcon(ImageLoader.java:40)
at com.reactnativenavigation.viewcontrollers.button.NavigationIconResolver.resolve(NavigationIconResolver.java:28)
at com.reactnativenavigation.viewcontrollers.TitleBarButtonController.applyNavigationIcon(TitleBarButtonController.java:100)
at com.reactnativenavigation.views.titlebar.TitleBar.setLeftButton(TitleBar.java:173)
at com.reactnativenavigation.views.titlebar.TitleBar.setBackButton(TitleBar.java:156)
at com.reactnativenavigation.views.topbar.TopBar.setBackButton(TopBar.java:209)
at com.reactnativenavigation.presentation.StackPresenter.applyButtons(StackPresenter.java:265)
at com.reactnativenavigation.presentation.StackPresenter.applyChildOptions(StackPresenter.java:134)
at com.reactnativenavigation.viewcontrollers.stack.StackController.applyChildOptions(StackController.java:87)
at com.reactnativenavigation.viewcontrollers.ViewController.lambda$onViewAppeared$0$ViewController(ViewController.java:199)
at com.reactnativenavigation.viewcontrollers.ViewController$$Lambda$0.run
at com.reactnativenavigation.viewcontrollers.ViewController.performOnParentController(ViewController.java:130)
at com.reactnativenavigation.viewcontrollers.ViewController.onViewAppeared(ViewController.java:197)
at com.reactnativenavigation.viewcontrollers.ChildController.onViewAppeared(ChildController.java:34)
at com.reactnativenavigation.viewcontrollers.ComponentViewController.onViewAppeared(ComponentViewController.java:35)
at com.reactnativenavigation.viewcontrollers.ViewController.onGlobalLayout(ViewController.java:249)
at android.view.ViewTreeObserver.dispatchOnGlobalLayout(ViewTreeObserver.java:981)
at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:2393)
at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1509)
at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:7051)
at android.view.Choreographer$CallbackRecord.run(Choreographer.java:927)
at android.view.Choreographer.doCallbacks(Choreographer.java:702)
at android.view.Choreographer.doFrame(Choreographer.java:638)
at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:913)
at android.os.Handler.handleCallback(Handler.java:751)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6692)
at java.lang.reflect.Method.invoke(Method.java)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1468)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1358)
We have an Android specific conditional in our index.js
import { AndroidBackArrow, BackIconBlack, XIconBlack, AndroidXIconBlack } from '../../../images'
const backArrowIcon = isIOS() ? BackIconBlack : AndroidBackArrow
...
Our images.js file has the actual requires:
const BackIconBlack = require('../assets/images/Back_Icon_Black.png')
const AndroidBackArrow = require('../assets/images/androidback.png')
I made the Android file lowercase after reading this SO: https://stackoverflow.com/questions/15320951/why-is-it-not-possible-to-use-uppercase-in-naming-resources-in-android
but it still does not work.
NOTE: We had this problem locally as well but found a workaround by putting the png in this directory:
/android/app/src/main/assets
and then in the images.js the require looked like this:
const AndroidBackArrow = require('../android/app/src/main/assets/androidback.png')
This only works locally it does not work when bundling an apk.
IOS works fine. No problems this is only an issue for Android.
[FILL THIS OUT]
Environment
- React Native Navigation version: 2.0.2641
- React Native version: 0.57.0
- Platform(s) (iOS, Android, or both?): Android only
- Device info (Simulator/Device? OS version? Debug/Release?):Device Release
This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. If you believe the issue is still relevant, please test on the latest Detox and report back. Thank you for your contributions.
The issue has been closed for inactivity.
Any solution to this issue. Right now I'm also facing the same issue. LeftButton Icons seems fine in iOS_Device, iOS_Simulator, and Android_Emulator but when it comes Android _Device my app is crashing. Here I'm attaching the Exception details. I need to show the hamburger menu icon on the left side for side menu.
java.lang.RuntimeException: java.io.FileNotFoundException: No content provider: assets_icon_sidemenu
at com.reactnativenavigation.viewcontrollers.button.NavigationIconResolver$1.onError(NavigationIconResolver.java:36)
at com.reactnativenavigation.utils.ImageLoader.loadIcon(ImageLoader.java:40)
at com.reactnativenavigation.viewcontrollers.button.NavigationIconResolver.resolve(NavigationIconResolver.java:28)
at com.reactnativenavigation.viewcontrollers.TitleBarButtonController.applyNavigationIcon(TitleBarButtonController.java:100)
at com.reactnativenavigation.views.titlebar.TitleBar.setLeftButton(TitleBar.java:173)
at com.reactnativenavigation.views.titlebar.TitleBar.setLeftButtons(TitleBar.java:168)
at com.reactnativenavigation.views.topbar.TopBar.setLeftButtons(TopBar.java:213)
at com.reactnativenavigation.presentation.StackPresenter.applyButtons(StackPresenter.java:259)
at com.reactnativenavigation.presentation.StackPresenter.applyChildOptions(StackPresenter.java:134)
at com.reactnativenavigation.viewcontrollers.stack.StackController.applyChildOptions(StackController.java:87)
at com.reactnativenavigation.viewcontrollers.ViewController.lambda$onViewAppeared$0(ViewController.java:199)
at com.reactnativenavigation.viewcontrollers.-$$Lambda$ViewController$vmZN1OooxS0255BXGGxtqDVZGzI.run(lambda)
at com.reactnativenavigation.viewcontrollers.ViewController.performOnParentController(ViewController.java:130)
at com.reactnativenavigation.viewcontrollers.ViewController.onViewAppeared(ViewController.java:197)
at com.reactnativenavigation.viewcontrollers.ChildController.onViewAppeared(ChildController.java:34)
at com.reactnativenavigation.viewcontrollers.ComponentViewController.onViewAppeared(ComponentViewController.java:35)
at com.reactnativenavigation.viewcontrollers.ViewController.onGlobalLayout(ViewController.java:249)
at android.view.ViewTreeObserver.dispatchOnGlobalLayout(ViewTreeObserver.java:912)
at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:2001)
at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1138)
at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:6154)
at android.view.Choreographer$CallbackRecord.run(Choreographer.java:792)
at android.view.Choreographer.doCallbacks(Choreographer.java:596)
at android.view.Choreographer.doFrame(Choreographer.java:557)
at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:778)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:155)
at android.app.ActivityThread.main(ActivityThread.java:5725)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1030)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:825)
Caused by: java.io.FileNotFoundException: No content provider: assets_icon_sidemenu
at android.content.ContentResolver.openTypedAssetFileDescriptor(ContentResolver.java:1131)
at android.content.ContentResolver.openAssetFileDescriptor(ContentResolver.java:986)
at android.content.ContentResolver.openInputStream(ContentResolver.java:711)
at com.reactnativenavigation.utils.ImageLoader.localFile(ImageLoader.java:120)
at com.reactnativenavigation.utils.ImageLoader.openStream(ImageLoader.java:112)
at com.reactnativenavigation.utils.ImageLoader.readJsDevImage(ImageLoader.java:77)
at com.reactnativenavigation.utils.ImageLoader.getDrawable(ImageLoader.java:67)
at com.reactnativenavigation.utils.ImageLoader.loadIcon(ImageLoader.java:38)
at com.reactnativenavigation.viewcontrollers.button.NavigationIconResolver.resolve(NavigationIconResolver.java:28)
at com.reactnativenavigation.viewcontrollers.TitleBarButtonController.applyNavigationIcon(TitleBarButtonController.java:100)
at com.reactnativenavigation.views.titlebar.TitleBar.setLeftButton(TitleBar.java:173)
at com.reactnativenavigation.views.titlebar.TitleBar.setLeftButtons(TitleBar.java:168)
at com.reactnativenavigation.views.topbar.TopBar.setLeftButtons(TopBar.java:213)
at com.reactnativenavigation.presentation.StackPresenter.applyButtons(StackPresenter.java:259)
at com.reactnativenavigation.presentation.StackPresenter.applyChildOptions(StackPresenter.java:134)
at com.reactnativenavigation.viewcontrollers.stack.StackController.applyChildOptions(StackController.java:87)
at com.reactnativenavigation.viewcontrollers.ViewController.lambda$onViewAppeared$0(ViewController.java:199)
at com.reactnativenavigation.viewcontrollers.-$$Lambda$ViewController$vmZN1OooxS0255BXGGxtqDVZGzI.run(lambda)
at com.reactnativenavigation.viewcontrollers.ViewController.performOnParentController(ViewController.java:130)
at com.reactnativenavigation.viewcontrollers.ViewController.onViewAppeared(ViewController.java:197)
at com.reactnativenavigation.viewcontrollers.ChildController.onViewAppeared(ChildController.java:34)
at com.reactnativenavigation.viewcontrollers.ComponentViewController.onViewAppeared(ComponentViewController.java:35)
at com.reactnativenavigation.viewcontrollers.ViewController.onGlobalLayout(ViewController.java:249)
at android.view.ViewTreeObserver.dispatchOnGlobalLayout(ViewTreeObserver.java:912)
at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:2001)
at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1138)
at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:6154)
at android.view.Choreographer$CallbackRecord.run(Choreographer.java:792)
at android.view.Choreographer.doCallbacks(Choreographer.java:596)
at android.view.Choreographer.doFrame(Choreographer.java:557)
at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:778)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:155)
at android.app.ActivityThread.main(ActivityThread.java:5725)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1030)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:825)
i'm also having this same issue.
export const appStack = {
root: {
sideMenu: {
left: {
component: {
name: DRAWER_CONTENT
}
},
center: {
stack: {
id: 'center',
children: [{
component: {
name: LAUNCH_SCREEN,
options: {
topBar: {
title: {
text: 'Welcome!',
color: Colors.snow
},
leftButtons: [
{
id: 'back',
icon: images.menuIcon,
// testID: 'menuButton'
}
]
}
}
}
}]
}
}
}
}
}
I've tried with base64 string, ico and png images, nothing works when i deploy to my Android device, only on simulators (both IOS and Android.)
I was facing a similar issue and I'm not experienced in Android development but I tracked down the issue to the localFile method in the ImageLoader.java file and did the following changes:
private static InputStream localFile(Context context, String uri) throws FileNotFoundException {
//return context.getContentResolver().openInputStream(Uri.parse(uri));
return context.getContentResolver().openInputStream(Uri.parse("android.resource://package.name/drawable/"+uri));
}
(Replace package.name with your package name)
That actually worked. I don't understand why this issue is not affecting most people. If someone with some Android knowledge could find the right way to do this (including getting the package name programmatically) please fix it and open a PR. Meanwhile, probably another alternative is to load images from the web with some package like react-native-vector-icons.
Generic code that fixed the problem for me:
private static InputStream localFile(Context context, String uri) throws FileNotFoundException {
return context.getContentResolver().openInputStream(Uri.parse("android.resource://"+context.getPackageName()+"/drawable/"+uri));
}
I don't understand why this issue is not affecting most people.
@EC-AI , this only happens if you use custom buildTypes. For instance, if you define a staging buildType. Last time I checked, that was the cause
We were having crashes and issues in development with our icons not being written out and appearing like "images_homeactive". This caused react-native-navigation to crash our app
This occurred when we upgraded to compileSDKVersion = 28 . Starting with Android 9 (API level 28), cleartext support is disabled by default
../app/src/main/AndroidManifest.xml
<application
...
android:usesCleartextTraffic="${isDebug}" tools:targetApi="28">
../android/app/build.gradle
buildTypes {
release {
...
manifestPlaceholders = [isDebug:false]
}
debug {
...
manifestPlaceholders = [isDebug:true]
}
}
Hope this helps someone.
Generic code that fixed the problem for me:
private static InputStream localFile(Context context, String uri) throws FileNotFoundException { return context.getContentResolver().openInputStream(Uri.parse("android.resource://"+context.getPackageName()+"/drawable/"+uri)); }
This answer saved my day. Thank you!