How does toga uses open_ File_ Dialog() on Android? #1990
-
The app uses main_window.open_file_dialog() to open files, which works normally on Ubuntu and Windows, but does not work on Android. The following prompt appears: Why is this and how to solve it? |
Beta Was this translation helpful? Give feedback.
Replies: 3 comments 7 replies
-
The Toga file dialog API isn't implemented on Android yet, but you can use the native APIs directly as shown at #1158 (comment). |
Beta Was this translation helpful? Give feedback.
-
This code works just fine with Android 15: async def openFileDialog(self, widget):
from toga.platform import current_platform
print("current_platform : ", current_platform)
if current_platform == "android":
from android.content import Intent
fileChose = Intent(Intent.ACTION_GET_CONTENT)
fileChose.addCategory(Intent.CATEGORY_OPENABLE)
fileChose.setType("*/*")
# Assuming `app` is your toga.App object
results = await self._impl.intent_result(Intent.createChooser(fileChose, "Choose a file"))
data = results['resultData'].getData()
context = self._impl.native
bytesJarray = bytes((context.getContentResolver().openInputStream(data).readAllBytes()))
# will do something to get filePath and return filePath
file_path = os.path.join(self.paths.cache, 'import_file')
with open(file_path, "wb") as file:
file.write(bytesJarray)
else:
filePath = await self.main_window.open_file_dialog("打开文件")
return filePath How can I use |
Beta Was this translation helpful? Give feedback.
-
您好,我已收到来信,会尽快给予你回复。
|
Beta Was this translation helpful? Give feedback.
Direct file access with those permissions doesn't work anymore in newer versions of Android, since they introduced "scoped storage".
Sorry about the
readAllBytes
error: I didn't realize that wasn't introduced until API level 33. I've edited the example to useread
in a loop.