0

Loading assets from Flutter in Java doesn't work if the path has spaces:

val assetPath2 = "sound_packs/packs/Blaze_Kit/Clap.wav"
val loader = FlutterInjector.instance().flutterLoader()
val key = loader.getLookupKeyForAsset(assetPath2)
val inputStream: InputStream = context.assets.open(key)
val assetFD: AssetFileDescriptor = context.assets.openFd(key)

With underscore:

val assetPath2 = "sound_packs/packs/Blaze_Kit/Clap.wav"

it worked

but for

val assetPath2 = "sound_packs/packs/Blaze Kit/Clap.wav"

it doesn't work:

JNI DETECTED ERROR IN APPLICATION: JNI NewStringUTF called with pending exception java.io.FileNotFoundException: flutter_assets/sound_packs/packs/Blaze Kit/Clap.wav
12-20 16:40:26.544 12887 12887 F zygote64: java_vm_ext.cc:523]   at long android.content.res.AssetManager.openAsset(java.lang.String, int) (AssetManager.java:-2)
12-20 16:40:26.544 12887 12887 F zygote64: java_vm_ext.cc:523]   at java.io.InputStream android.content.res.AssetManager.open(java.lang.String, int) (AssetManager.java:374)
12-20 16:40:26.544 12887 12887 F zygote64: java_vm_ext.cc:523]   at java.io.InputStream android.content.res.AssetManager.open(java.lang.String) (AssetManager.java:348)
12-20 16:40:26.544 12887 12887 F zygote64: java_vm_ext.cc:523]   at boolean com.example.flutter_app.JNI$Companion.loadWavAsset(java.lang.String, java.lang.String, float) (JNI.kt:47)

This is the pubspec.yaml:

assets:
    - 'sound_packs/packs/Blaze Kit/'
    - 'sound_packs/packs/Blaze_Kit/'

I have the 2 folders named Blaze Kit and Blaze_Kit with the same file Clap.wav inside of it.

Is this a bug or desired behaviour? Is there a way to encode the space in the path for the assets?

nawkljbcoakjbco
  • 203
  • 3
  • 11
Gatonito
  • 1,662
  • 5
  • 26
  • 55
  • Does this answer your question? [Read file with whitespace in name using Java?](https://stackoverflow.com/questions/23828105/read-file-with-whitespace-in-name-using-java) – Yadu Dec 21 '20 at 06:57

1 Answers1

0

I believe this is intended. Reading filenames terminates on the last character, so a space in the filename (That includes folders) would terminate the file read on that space. Best just to use an underscore or dash.

Scott Godfrey
  • 641
  • 5
  • 8