简体   繁体   English

“gomobile build”如何创建文件和写入文件(安装在三星平板电脑上的外部micro-SD卡)(在APK中)?

[英]“gomobile build” how to create files and write to files (external micro-SD card installed on Samsung tablet) (in APK)?

Original quetion:原问题:

Does anybody know how does gomobile build (not bind) support FileSystem?有谁知道gomobile构建(不绑定)如何支持文件系统? I was able to use the asset package for opening and reading files as discussed in this discussion .我能够使用资产包来打开和读取文件,如本次讨论中所讨论的

But not sure how I can create an write to new files in an APK file generated by gomobile I am able to run the following ample code in Go and verify that the "testFile.txt" is created with the provided message.但不确定如何在由 gomobile 生成的 APK 文件中创建对新文件的写入我能够在 Go 中运行以下充足的代码并验证是否使用提供的消息创建了“testFile.txt”。

message := []byte("Hello, testing!")
err := ioutil.WriteFile("testFile.txt", message, 0777)
if err != nil {
   fmt.Println(err)
}

But after generating APK file and running it on a tablet (Android) I get the file access error as below and the "testFile.txt" is not created.但是在生成 APK 文件并在平板电脑 (Android) 上运行后,我收到如下文件访问错误,并且未创建“testFile.txt”。

I/GoLog: open testFile.txt: read-only file system I/GoLog:打开 testFile.txt:只读文件系统

Any hint is appreciated.任何提示表示赞赏。

Updated my question based on this instruction :根据此说明更新了我的问题:

What version of Go are you using ('go version')?您使用的是哪个版本的 Go(“go 版本”)?

$ go version
go version go1.16.3 windows/amd64

Does this issue reproduce with the latest release?这个问题会在最新版本中重现吗?

NA不适用

What operating system and processor architecture are you using ('go env')?您使用的是什么操作系统和处理器架构('go env')?

Windows 10 Pro视窗 10 专业版

go env Output
$ go env

set GO111MODULE=auto
set GOARCH=amd64
set GOBIN=
set GOCACHE=C:\Users\instu\AppData\Local\go-build
set GOENV=C:\Users\instu\AppData\Roaming\go\env
set GOEXE=.exe
set GOFLAGS=
set GOHOSTARCH=amd64
set GOHOSTOS=windows
set GOINSECURE=
set GOMODCACHE=C:\Users\instu\OneDrive\Desktop\go-workSpace\pkg\mod
set GONOPROXY=
set GONOSUMDB=
set GOOS=windows
set GOPATH=C:\Users\instu\OneDrive\Desktop\go-workSpace
set GOPRIVATE=
set GOPROXY=https://proxy.golang.org,direct
set GOROOT=C:\Projects\Go
set GOSUMDB=sum.golang.org
set GOTMPDIR=
set GOTOOLDIR=C:\Projects\Go\pkg\tool\windows_amd64
set GOVCS=
set GOVERSION=go1.16.3
set GCCGO=gccgo
set AR=ar
set CC=gcc
set CXX=g++
set CGO_ENABLED=1
set GOMOD=C:\Users\instu\OneDrive\Desktop\go-workSpace\src\Minicloud\go.mod
set CGO_CFLAGS=-g -O2
set CGO_CPPFLAGS=
set CGO_CXXFLAGS=-g -O2
set CGO_FFLAGS=-g -O2
set CGO_LDFLAGS=-g -O2
set PKG_CONFIG=pkg-config
set GOGCCFLAGS=-m64 -mthreads -fmessage-length=0 -fdebug-prefix-map=C:\Users\instu\AppData\Local\Temp\go-build2510967962=/tmp/go-build -gno-record-gcc-switches

What did you do?你做了什么?

Described in original question section, but in summary I need to add a function which is able to write logs into an installed micro-SD card on the Samsung S6 lite tablet.在原始问题部分中描述,但总而言之,我需要添加一个功能,该功能能够将日志写入三星 S6 lite 平板电脑上已安装的 micro-SD 卡。 Here is the sample code i followed to write a file in Go.这是我在 Go 中编写文件所遵循的示例代码。 it works when i run go, but i experience the read-only error message after generating and installing APK file on Tablet当我运行 go 时它工作,但我在平板电脑上生成和安装 APK 文件后遇到只读错误消息

What did you expect to see?你期待看到什么?

Expect that Android app (installed as an APK file generated by gomobile build) is able to create a file and write to the created file期望Android应用程序(安装为由gomobile build生成的APK文件)能够创建文件并写入创建的文件

What did you see instead?你看到了什么?

read-only error message.只读错误消息。

I/GoLog: open testFile.txt: read-only file system I/GoLog:打开 testFile.txt:只读文件系统

This sample code works for me (to write to internal storage):此示例代码适用于我(写入内部存储):

func storeLog(fileDir string, fileName string, msg string) {
    msgByte := []byte(msg)

    file, err := os.Create(fileDir + fileName)

    if err != nil
      fmt.Printf(err)
    defer file.Close()
    numB, err := file.Write(msgByte)

    if err != nil
      fmt.Printf(err)
enter code here
    fmt.Printf("wrote %d bytes\n", numB)
}

Just need to make sure to :只需要确保:

  • Find the appropriate fileDir:找到合适的fileDir:

adb -s <deviceID> shell echo $EXTERNAL_STORAGE adb -s <deviceID> shell echo $EXTERNAL_STORAGE

  • Use the following command to find your deviceID使用以下命令查找您的 deviceID

adb devices亚行设备

  • Add user permission for writing to file in AndroidManifest.xml file在 AndroidManifest.xml 文件中添加写入文件的用户权限

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM