Golang:Gomobile 应用无法生成文件。

huangapple go评论85阅读模式
英文:

Golang: Gomobile app cannot generate files

问题

有人之前使用过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应用程序生成过文件吗?

附注:我尝试将目录指定为"/storage/emulated/0/Go/",这与我存储apk文件的位置相同,但没有成功。

英文:

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

得分: 3

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

如果您没有权限,请创建一个AndroidManifest.xml文件。您可以使用-v标志查看gomobile自动为您创建的文件的内容(gomobile build -v)。

<manifest></manifest>标签之间添加以下行。

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

再次构建应用程序。确保gomobile使用您的清单文件(使用-v标志)。我能够通过os.Create("/sdcard/zzz.txt")创建文件。我还需要这个来进行调试,所以我不介意写入特定的位置,即我的SD卡。
当然,在您的手机上,位置可能会改变。

此外,如果您只想要一些日志,您可以安装Android Debug Bridge并使用adb logcat。要从您的Go应用程序中过滤出日志,可以使用adb logcat | grep "I/GoLog"

英文:

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 &lt;manifest&gt; and &lt;/manifest&gt; tags.

&lt;uses-permission android:name=&quot;android.permission.WRITE_EXTERNAL_STORAGE&quot; /&gt;

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(&quot;/sdcard/zzz.txt&quot;). 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 &quot;I/GoLog&quot; would work.

huangapple
  • 本文由 发表于 2015年11月20日 06:28:44
  • 转载请务必保留本文链接:https://go.coder-hub.com/33815541.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定