Repository navigation
Handling android file intent in app #6754
Replies: 1 comment 2 replies
|
Hi Nik, good news: this is more achievable than the older threads suggest, and pyjnius is supported in Flet, contrary to what a lot of search results will tell you. There are two halves to this, and they are solved in different ways. 1. Declaring the intent filter (build time)Android decides which apps appear in the "Open with" list by reading Flet handles this through <intent-filter>
<action android:name="android.intent.action.VIEW" />
<category android:name="android.intent.category.DEFAULT" />
<data android:mimeType="text/plain" />
</intent-filter>
<intent-filter>
<action android:name="android.intent.action.SEND" />
<category android:name="android.intent.category.DEFAULT" />
<data android:mimeType="text/plain" />
</intent-filter>2. Reading the incoming intent (runtime)This is where pyjnius comes in, and it does work in Flet. See the official post: Tap into native Android and iOS APIs with Pyjnius and Pyobjus. Add pyjnius as an Android-only dependency in from jnius import autoclass, cast
def read_incoming_file():
PythonActivity = autoclass("org.kivy.android.PythonActivity")
activity = cast("android.app.Activity", PythonActivity.mActivity)
intent = activity.getIntent()
uri = intent.getData()
if uri is None:
return None
stream = activity.getContentResolver().openInputStream(uri)
# read the stream into bytes, then decode as textThree things that will bite youIn rough order of likelihood: Use Handle the cold start case. Your app may be launched by the intent rather than already running, so read Verify the activity class name. The On release builds and R8If you hit issues that only appear in a release build, that is usually R8 minification renaming classes that Flet's Worth testing a release build on a real device before shipping, since this class of problem does not show up in debug. On issue #6671Still worth a thumbs up for first class support, since the manifest and intent handling would be much nicer as a proper Flet API. But you should not be blocked in the meantime. Hope that helps, and good luck with the app. |
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
Question
Hello,
I started building android apps with Flet about three months ago, so a newbie. I would like my app to access/display the contents of a text file when a text file is selected in the file manager and directed to my app. I understand this is called handling the "android intent". I have spent quite some time on google to try to find a solution to this problem and have not got a clear answer if this doable in pure python or not. Is there an example where this has been implemented which I can use as a starting point?
Thank you,
Nik.
PS: I would like to add that I am enjoying building android apps using the Flet project a lot. Appreciate the work that the developers have put in to make this possible.
Code sample
No response
Error message
No response
------------------------------------------------------
All reactions