1
package component

import (
    "encoding/json"
    "io/ioutil"
    "os"
)


type LastComponent struct {
    Name string
}

const fname = "componentfiles"


func Persist(comp string) string {
    lcomp := LastComponent{Name: comp}
    b, err := json.Marshal(lcomp)
    if err != nil {
        return "err-MARSHAL"
    }
    file, err := os.Create(fname)
    if err != nil {
        return "err-CREATE-FILE"
    }
    defer file.Close()
    _, err = file.Write(b)
    if err != nil {
        return "err-FILE-WRITE-PROB"
    }
    return ""
}

func Component() string {
    f, err := os.Open(fname)
    if err != nil {
        return "err-FILE-NOT-OPEN"
    }
    defer f.Close()
    b, err := ioutil.ReadAll(f)
    if err != nil {
        return ""
    }
    var v LastComponent
    json.Unmarshal(b, v)
    return v.Name
}


}

The code above works fine and so does the javascript side of code. I keep receiving err-CREATE-FILE inside my javascript. So os.Create and os.Open are not working as expected.

Although it is an internal storage, permissions are not required, but I also turned on the permissions in manifest file, but with no avail.

What could be the correct way to Open and Create files in android using gomobile when using along side React Native?

Update:

In adb logcat, I keep getting this all over the place

E/Vold ( 276): Failed to find mounted volume for /storage/sdcard1/Android/data/com.gotest/cache/

Minty
  • 1,163
  • 11
  • 18

1 Answers1

0

So you should have some success if you pass this in as a parameter - something like the following is working for me:

go:

func Component(folderPath string) string {
    f, err := os.Open(path.Join(folderPath, fname)) 
    ...

Java:

Component.Component(getApplicationContext().getFilesDir().getAbsolutePath())

Alternatively, you could use something like getExternalStorageDirectory().getAbsolutePath(). They key is that you need to get somewhere storagewise that is writable by your process/user.

Dan Field
  • 20,885
  • 5
  • 55
  • 71