uri_android.go 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. //go:build android
  2. // +build android
  3. package mobile
  4. /*
  5. #cgo LDFLAGS: -landroid -llog
  6. #include <stdlib.h>
  7. char* contentURIGetFileName(uintptr_t jni_env, uintptr_t ctx, char* uriCstr);
  8. */
  9. import "C"
  10. import (
  11. "path/filepath"
  12. "unsafe"
  13. "fyne.io/fyne/v2"
  14. "fyne.io/fyne/v2/internal/driver/mobile/app"
  15. "fyne.io/fyne/v2/storage"
  16. )
  17. type androidURI struct {
  18. systemURI string
  19. fyne.URI
  20. }
  21. // Override Name on android for content://
  22. func (a *androidURI) Name() string {
  23. if a.Scheme() == "content" {
  24. result := contentURIGetFileName(a.systemURI)
  25. if result != "" {
  26. return result
  27. }
  28. }
  29. return a.URI.Name()
  30. }
  31. func (a *androidURI) Extension() string {
  32. return filepath.Ext(a.Name())
  33. }
  34. func contentURIGetFileName(uri string) string {
  35. uriStr := C.CString(uri)
  36. defer C.free(unsafe.Pointer(uriStr))
  37. var filename string
  38. app.RunOnJVM(func(_, env, ctx uintptr) error {
  39. fnamePtr := C.contentURIGetFileName(C.uintptr_t(env), C.uintptr_t(ctx), uriStr)
  40. vPtr := unsafe.Pointer(fnamePtr)
  41. if vPtr == C.NULL {
  42. return nil
  43. }
  44. filename = C.GoString(fnamePtr)
  45. C.free(vPtr)
  46. return nil
  47. })
  48. return filename
  49. }
  50. func nativeURI(uri string) fyne.URI {
  51. return &androidURI{URI: storage.NewURI(uri), systemURI: uri}
  52. }