生成“.APK"后无法识别文件;使用 gomobile 工具从 Go 到 Android 的文件 [英] File is not recognized after generating the ".APK" file from Go to Android using gomobile tool

查看:37
本文介绍了生成“.APK"后无法识别文件;使用 gomobile 工具从 Go 到 Android 的文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发一个项目,在该项目中我需要使用 gomobile 工具.计划是能够在安卓平板电脑上运行它.当我在平板电脑上安装 apk 文件时,通过 Android Studio 出现以下错误.

 E/Go: panic: open C:/Users/ash/OneDrive/Desktop/go-workSpace/src/Myproject/config.json: 没有那个文件或目录

但是,在点击错误消息中的给定路径后,Android Studio 会打开它抱怨要查找的文件 (config.json)

这是我在 Go 中的源代码

exDir := "C:/Users/ash/OneDrive/Desktop/go-workSpace/src/Myproject/";configFile, err := ioutil.ReadFile(filepath.Join(filepath.Dir(exDir), "config.json"))//在可执行文件所在的目录中查找配置文件检查(错误)功能检查(错误错误){如果错误!= nil {fmt.Println(收到通用错误,惊慌失措")fmt.Println(err)恐慌(错误)}}

有什么建议可以修复文件路径吗?

解决方案

查看此内容后 线程.这是官方支持页面.所以,我想我需要

  1. 将所有文件移动到名为 assets 的目录中.
  2. assets 目录嵌入到.APK"中.可以通过 Android 代码库访问.

<块引用>

导入golang.org/x/mobile/asset";jsonFile, errOpen := asset.Open(config.json)如果 errOpen != nil {fmt.Println(errOpen)}推迟 jsonFile.Close()buf, errRead := ioutil.ReadAll(jsonFile)如果 errRead != nil {fmt.Println(errRead)}

I am working on a project in which i need to generate an APK file form a Golang code base using gomobile tool. the plan is to be able to run it on android tablet. When I install the apk file on the tablet, through the Android Studio I get the following error.

 E/Go: panic: open C:/Users/ash/OneDrive/Desktop/go-workSpace/src/Myproject/config.json: no such file or directory

However, after clicking on the given path in the error message, the Android Studio opens the the file it is complaining to find (config.json)

Here is the source code I have in Go

exDir := "C:/Users/ash/OneDrive/Desktop/go-workSpace/src/Myproject/"    
configFile, err := ioutil.ReadFile(filepath.Join(filepath.Dir(exDir), "config.json")) // Look for the config file in the same directory as the executable
    
Check(err)

func Check(err error) {
    if err != nil {
        fmt.Println("Received generic error, panicking")
        fmt.Println(err)
        panic(err)
    }
}

Any suggestion how to fix the file path?

解决方案

After reviewing this thread. Here is the official go support page. So, I think I need to

  1. move all the files into a directory called assets.
  2. The assets directory is embedding to the ".APK" can be accessed through Android code base.

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

jsonFile, errOpen := asset.Open(config.json)
if errOpen != nil {
    fmt.Println(errOpen)
}
defer jsonFile.Close()
buf, errRead := ioutil.ReadAll(jsonFile)
if errRead != nil {
    fmt.Println(errRead)
}

这篇关于生成“.APK"后无法识别文件;使用 gomobile 工具从 Go 到 Android 的文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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