The DataDir plugin provides access to standard Android application directories and supports reading and writing files within internal storage.
Setup
The datadir plugin is already registered in the default AppTemplate.
API
datadir.NewPlugin() *DataDirPlugin
Creates a new datadir plugin instance.
(*DataDirPlugin).GetDirs() (AppDirs, error)
Returns the application directory paths.
type AppDirs struct {
Files string // Internal files directory
Cache string // Internal cache directory
ExternalFiles string // External files directory (if available)
ExternalCache string // External cache directory (if available)
}
(*DataDirPlugin).ReadFile(path string) (string, error)
Reads the contents of a file from the internal files directory.
| Argument | Type | Description |
|---|---|---|
path |
string |
Relative path inside internal files directory |
Returns: File content as string, or error
(*DataDirPlugin).WriteFile(path string, content string) error
Writes content to a file in the internal files directory. Creates parent directories if needed.
| Argument | Type | Description |
|---|---|---|
path |
string |
Relative path inside internal files directory |
content |
string |
Text content to write |
Returns: error
(*DataDirPlugin).FileExists(path string) (bool, error)
Checks whether a file exists in the internal files directory.
| Argument | Type | Description |
|---|---|---|
path |
string |
Relative path inside internal files directory |
Returns: (exists bool, err error)
(*DataDirPlugin).DeleteFile(path string) error
Deletes a file from the internal files directory.
| Argument | Type | Description |
|---|---|---|
path |
string |
Relative path inside internal files directory |
Returns: error
Example
import "github.com/sweet-juice/sweetjuice/plugins/datadir"
plugin := datadir.NewPlugin()
// Get directory paths
dirs, _ := plugin.GetDirs()
fmt.Println("Files dir:", dirs.Files)
// Write a file
plugin.WriteFile("notes.txt", "Hello Sweet Juice")
// Read it back
content, _ := plugin.ReadFile("notes.txt")
fmt.Println("File content:", content)
// Check existence
exists, _ := plugin.FileExists("notes.txt")
fmt.Println("Exists:", exists)
// Delete it
plugin.DeleteFile("notes.txt")
Notes
- All file operations use the app’s private internal files directory (
getFilesDir()). - External directories are available via
GetDirs(), but file read/write methods operate on internal storage only. - Parent directories are created automatically when writing files.
- No extra permissions are required for internal storage operations.