forked from remogatto/mandala
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathresource_android.go
More file actions
49 lines (42 loc) · 1003 Bytes
/
resource_android.go
File metadata and controls
49 lines (42 loc) · 1003 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
// +build android
package mandala
import (
"archive/zip"
"fmt"
"io"
"path/filepath"
"unsafe"
)
// #include <android/native_activity.h>
// #include "resource_android.h"
import "C"
var (
// The path in which the framework will search for resources.
ResourcePath string = "res"
)
func loadResource(activity unsafe.Pointer, filename string) ([]byte, error) {
apkPath := C.GoString(C.getPackageName((*C.ANativeActivity)(activity)))
// Open a zip archive for reading.
r, err := zip.OpenReader(apkPath)
if err != nil {
return nil, err
}
defer r.Close()
// Iterate through the files in the archive.
for _, f := range r.File {
if f.Name == filepath.Join(ResourcePath, filename) {
rc, err := f.Open()
if err != nil {
return nil, err
}
buffer := make([]byte, f.UncompressedSize64)
_, err = io.ReadFull(rc, buffer)
if err != nil {
return nil, err
}
rc.Close()
return buffer, nil
}
}
return nil, fmt.Errorf(`Resource "%s" was not found!`, filename)
}