• After 15+ years, we've made a big change: Android Forums is now Early Bird Club. Learn more here.

Accessing assets folder from within a fragment

PeterF75

Lurker
Hi.
I'm trying to use inputStream in a fragment but having difficulty with the usage.
Works fine in an activity. Please help. The issue in the code below is the line that accesses the assets folder.
Thanks

import android.os.Bundle
import androidx.fragment.app.Fragment
import android.view.LayoutInflater
import android.view.View
import android.webkit.WebView
import android.view.ViewGroup
import kotlinx.android.synthetic.main.fragment_home.*
import java.io.IOException
import java.io.InputStream


public var message:String = ""

class Home : Fragment(R.layout.fragment_home) {


override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)

val mimeType: String = "text/html"
val utfType: String = "UTF-8"
readFile()
webViewHome.setVerticalScrollBarEnabled(true)
webViewHome.loadData(message, mimeType, utfType)
}

fun readFile() {
var string: String = ""
try {
val inputStream: InputStream = assets.open("sample.html")
val size = inputStream.available()
val buffer = ByteArray(size)
inputStream.read(buffer)
string = String(buffer)
} catch (e: IOException) {
e.printStackTrace()
}
message = string
}

}
 
Back
Top Bottom