1

有没有人用过 gomobile 应用并成功在手机中创建文件?我在装有 Android 4.4.2 的 Galaxy S4 上尝试了以下代码:

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()
            }
        }
    })
}

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

我还尝试了一个名为“AnGoIde”的应用程序,它允许我们直接在 Android 中编写 Go 和编译,以下代码能够创建“zzz.txt”文件:

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

最终我想将所有错误保存在一个文件中,这样我就可以看到导致我的应用程序崩溃的原因,而 AnGoIde 不支持很多包,所以我不能将它用于我的测试。有没有人以前用 gomobile 应用程序成功生成过文件?

ps 我尝试将目录指定为“/storage/emulated/0/Go/”,这是我存储 apk 文件但不起作用的同一个位置。

4

1 回答 1

2

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

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.

于 2015-11-20T05:32:42.463 回答