最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

kotlin - pink screen in touchable areas problem in running HTML 5 game in android webview - Stack Overflow

programmeradmin2浏览0评论

pink screen in touchable areas problem in running HTML 5 game in android webview

  1. game in assets folder
  2. local server starts.
  3. shows pink screen in webview and also in external browser in chrome

I think the problem is with the making the server side files the code is

how to solve the issue?class LocalServer(private val context: MainActivity, port: Int) : NanoHTTPD(port) {

override fun serve(session: IHTTPSession): Response {
    return try {
        var uri = session.uri
        if (uri == "/") uri = "/index.html" // Default to index.html if root is accessed

        // Log the file path for debugging
        Log.d("LocalServer", "Accessing file: game$uri")

        val filePath = "game$uri"  // Correct the path to look for files in the 'game' folder inside assets

        // Check if the file exists in assets
        val inputStream: InputStream = context.assets.open(filePath)
        val buffer = ByteArray(inputStream.available())
        inputStream.read(buffer)
        inputStream.close()

        // Log successful file access
        Log.d("LocalServer", "Successfully served: $filePath")

        newFixedLengthResponse(Response.Status.OK, getMimeType(uri), String(buffer))
    } catch (e: IOException) {
        Log.e("LocalServer", "File not found: ${e.message}")
        newFixedLengthResponse(Response.Status.NOT_FOUND, "text/plain", "404 Not Found")
    }
}

private fun getMimeType(uri: String): String {
    return when {
        uri.endsWith(".html") -> "text/html"
        uri.endsWith(".js") -> "application/javascript"
        uri.endsWith(".css") -> "text/css"
        uri.endsWith(".png") -> "image/png"
        uri.endsWith(".jpg") -> "image/jpeg"
        uri.endsWith(".gif") -> "image/gif"
        else -> "application/octet-stream"
    }
}
发布评论

评论列表(0)

  1. 暂无评论