gomobile如何在apk中嵌入资产 [英] gomobile how to embed assets in apk

查看:26
本文介绍了gomobile如何在apk中嵌入资产的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用 gomobile for Android 进行开发时.如何在 apk 文件中包含资产(html、js、图像)?我有一个在 apk 内部运行的网络服务器,它在 apk 和 go run 中工作,但是当打包在手机上的 apk 中时,该服务找不到它的资产/静态文件.

When developing with gomobile for Android. How can I include assets (html, js, images) inside the apk file? I have a web server running inside the apk which works inside the apk as well as with go run, but when packaged inside the apk on the phone the service cannot find it's assets/static files.

推荐答案

您必须将您的资产放在名为assets"的目录中.

You must put your assets within a directory called 'assets'.

看这个例子:

https://github.com/udhos/fugo/tree/master/demo/invader/assets

然后你就可以使用包golang.org/x/mobile/asset 加载所需资源:

Then you can use the package golang.org/x/mobile/asset to load the desired asset:

import "golang.org/x/mobile/asset"

f, err := asset.Open("image.png")

示例代码取自 https://github.com/udhos/fugo/blob/master/demo/invader/main.go:

func loadFull(name string) ([]byte, error) {
    f, errOpen := asset.Open(name)
    if errOpen != nil {
        return nil, errOpen
    }
    defer f.Close()
    buf, errRead := ioutil.ReadAll(f)
    if errRead != nil {
        return nil, errRead
    }
    log.Printf("loaded: %s (%d bytes)", name, len(buf))
    return buf, nil
}

这篇关于gomobile如何在apk中嵌入资产的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆