Golang:Gomobile应用程序无法生成文件 [英] Golang: Gomobile app cannot generate files

查看:387
本文介绍了Golang:Gomobile应用程序无法生成文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

以前是否有人使用gomobile应用程序,并已成功在手机中创建文件?我在Galaxy S4上用Android 4.4.2试过了下面的代码:

  package main 
import(
golang.org/x/mobile/app
golang.org/x/mobile/event/lifecycle
golang.org/x/mobile/event/paint
os

func main(){
os.Create(zzz.txt)
app.Main(func(a app.App){
对于e:=范围a.Events(){
switch e:= a.Filter(e)。(type){
case lifecycle.Event:
_ = e
case paint.Event:
a.Publish()
}
}
})
}

但是手机中没有创建任何文件。



我也尝试过一款名为AnGoIde的应用程序,它允许我们编写Go并在Android中直接编译,以下代码可以创建zzz.txt文件:

  package main 
importos
func main(){
os.Create(zzz.txt)
}

最后,我想将所有错误保存在一个文件中,以便查看导致我的应用程序崩溃的原因,并且AnGoIde不支持许多软件包,因此我无法使用它我的测试。有没有人成功地使用gomobile应用生成文件?

p.s。我尝试将目录指定为/ storage / emulated / 0 / Go /,这是我存储apk文件的同一个地方,但无效。 解决方案

要写入文件,您的应用需要某种形式的权限。



如果您没有创建 AndroidManifest.xml 。您可以使用 -v 标志自动为您创建文件 gomobile 的内容。 ( gomobile build -v



< manifest> < / manifest> 标签。



<使用权限android:name =android.permission.WRITE_EXTERNAL_STORAGE/>



再次构建应用程序。确保 gomobile 使用您的清单文件使用 -v 标志。我能够通过 os.Create(/ sdcard / zzz.txt)创建文件。我也需要这个进行调试,所以我不介意写一个特定的位置,即我的SD卡。
当然,在您的手机中,位置可能会发生变化。另外,如果你只是想要一些日志,你可以安装Android调试桥,并使用 logcat.htmlrel =nofollow> adb logcat 。从您的Go应用程序中过滤掉日志 adb logcat | grepI / GoLog可以工作。


Does anyone use gomobile app before and have successfully create files in phones? I tried the following code on Galaxy S4 with Android 4.4.2:

package main
import (
    "golang.org/x/mobile/app"
    "golang.org/x/mobile/event/lifecycle"
    "golang.org/x/mobile/event/paint"
    "os"
)
func main() {
    os.Create("zzz.txt")
    app.Main(func(a app.App) {
        for e := range a.Events() {
            switch e := a.Filter(e).(type) {
            case lifecycle.Event:
                _=e
            case paint.Event:
                a.Publish()
            }
        }
    })
}

However no file is created in the phone.

I also tried an app called "AnGoIde" which allow us to write Go and compile directly in Android, and the following code is able to create the "zzz.txt" file:

package main
import "os"
func main(){
    os.Create("zzz.txt")
}

Eventually I would like to save all errors in a file so I can see what cause my apps to crash, and AnGoIde doesn't support many packages so I cannot use it for my tests. Does anyone successfully generated files with gomobile apps before?

p.s. I tried to specify the directory to "/storage/emulated/0/Go/" which is the same place I store the apk file but doesn't work.

解决方案

To write to a file your app needs some form of permission.

If you don't have one create an AndroidManifest.xml. You can see the content of the file gomobile automatically create for you using the -v flag. (gomobile build -v)

Add the following line between <manifest> and </manifest> tags.

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />

Build the app again. Make sure gomobile is using your manifest file using the -v flag. I was able to create the file by os.Create("/sdcard/zzz.txt"). I also needed this for debugging so I didn't mind writing to a specific location, namely my sdcard. Of course in your phone the location may change.

Additionally, If you just want some logs you can install Android Debug Bridge and use adb logcat. To filter out the logs from your Go app adb logcat | grep "I/GoLog" would work.

这篇关于Golang:Gomobile应用程序无法生成文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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