Golang:Gomobile应用无法生成文件

是否有人以前使用过gomobile应用并成功在手机中创建文件? 我在装有Android 4.4.2的Galaxy S4上尝试了以下代码:</ p>

 包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){
案例生命周期 事件:
_ = e
案例绘画事件:
a.Publish()
}
}
})
}
</ code> </ pre>

< p>但是手机中没有创建文件。</ p>

我还尝试了一个名为“ AnGoIde”的应用,该应用允许我们编写Go并直接在Android中进行编译,并且以下代码能够 创建“ zzz.txt”文件:</ p>

 包main 
import“ os”
func main(){
os.Create(“ zzz.txt”)
}
</ code> </ pre>

最终,我想将所有错误保存在文件中,以便查看导致我的应用崩溃的原因,而AnGoIde不支持man y包,因此我无法将其用于测试。 </ p>

p.s。之前有没有人成功使用gomobile应用成功生成文件? 我试图将目录指定为“ / storage / emulated / 0 / Go /”,该目录与我存储apk文件的位置相同,但是不起作用。</ p>
</ div>

展开原文

原文

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.

1个回答

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.

douhuireng4407
douhuireng4407 该解决方案的第3步可以提取xml文件。 修改完成后,将修改后的xml放在.go文件的同一目录下,并像往常一样进行编译,gomobile将与新的xml文件一起编译。
接近 5 年之前 回复
dourou9477
dourou9477 stackoverflow.com/questions/3593420/...
接近 5 年之前 回复
doupu3635
doupu3635 对于正在寻找解决方案的人们的一些评论:
接近 5 年之前 回复
duanna3634
duanna3634 谢谢你的工作!!!
接近 5 年之前 回复
Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问
相关内容推荐