简体   繁体   中英

“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? 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.

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.

I/GoLog: open testFile.txt: read-only file system

Any hint is appreciated.

Updated my question based on this instruction :

What version of Go are you using ('go version')?

$ 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')?

Windows 10 Pro

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. Here is the sample code i followed to write a file in Go. it works when i run go, but i experience the read-only error message after generating and installing APK file on Tablet

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

What did you see instead?

read-only error message.

I/GoLog: open testFile.txt: read-only file system

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:

adb -s <deviceID> shell echo $EXTERNAL_STORAGE

  • Use the following command to find your deviceID

adb devices

  • Add user permission for writing to file in AndroidManifest.xml file

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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