Minimum code to load and display an epub #332
-
I know there are other threads here about this same topic but they are a few years old. class MainActivity : AppCompatActivity() {
private val coroutineScope: CoroutineScope = MainScope()
var publication: Publication? = null
override fun onCreate(savedInstanceState: Bundle?) {
// this line crashes but is in the example https://github.com/readium/kotlin-toolkit/issues/234
supportFragmentManager.fragmentFactory = EpubNavigatorFragment.createFactory(publication = publication!!)
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
val streamer = Streamer(this)
val asset = FileAsset(File("/storage/emulated/0/Download/book.epub")) // "file:///android_asset/book.epub" both of these paths return errors, file is corrupt and file is not found
coroutineScope.async {
publication = streamer.open(asset, allowUserInteraction = true, sender = this)
.getOrElse { error ->
Log.e("error", error.getUserMessage(this@MainActivity))
return@async
}
}
val fragment = supportFragmentManager.fragmentFactory.instantiate(classLoader, EpubNavigatorFragment::class.java.name)
supportFragmentManager.beginTransaction().apply {
replace(R.id.myframe, fragment) // myframe is a FrameLayout
commit()
}
}
} Is there other documentation I may have overlooked? I have of course read through the test app but didn't find it very enlightening. edit: |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 1 reply
-
It's crashing because you are force-unwrapping the You need to first open the publication with the streamer, and then create a fragment factory from it. Usually you would:
|
Beta Was this translation helpful? Give feedback.
It's crashing because you are force-unwrapping the
publication
optional when it's not yet set.You need to first open the publication with the streamer, and then create a fragment factory from it. Usually you would:
Publication
with the streamer in a bookshelf activity.Publication
object in a shared store (seeReaderRepository
in the test app)