[Interest] [ Android ] How make Qt widget app open file using android.intent.action.VIEW ?
baiduzhyi.devel at gmail.com
Wed Apr 8 12:09:20 CEST 2015
On Monday 06 April 2015 18:06:16 Eddie Sutton wrote:
> My Qt 5.4.2 widget app handles opening of file with *.TSL on Windows from
> the File Explorer or email file attachments.
> How do I do same on Android?
> How do I pass the file name using Android intent action view so that the Qt
> app can open and view?
> Examples are much appreciated.
> Thanks in advance,
Yea, I've been trying to do that for quite some time. Here is the short
summary of my discoveries:
1. At the moment, command line arguments (read: data from intent) is not
passed to the application. That can be fixed with couple of lines of code in
QtNatvie.java, not a big problem.
2. Soem apps are providing URI in form of "content://downloader/file_1" or
something like that. So you need additional java code block in QtNative.java
to handle that transformation into real file system URI. Again, not a big
problem, just some mess with different android API versions.
3. Then comes the problem of application initialization. In your Qt
application, you expect to get command line arguments in main method, and you
can get those arguments in your QML code as well, accessing Qt.arguments.
Which means that first launch of your application goes fine. Problem starts
when you're mixing Android-way with C++-way. Android does not kill and restart
application unless it runs out of memory or you're killing the app manually. I
was not able to make the app kill itself from QML, maybe that could be a
solution. But, when you're opening your app for the first time you can get
your command line arguments, when you're doing that for the second time - it
opens already working app, you're not getting into C++ main and your QML app
does not have those arguments. This requires little scarier patch, changing
the way native library is loaded and main is initialized. Unfortunately, my
C++ knowledge is not enough to make it work properly, but I've got some almost
4. I've stopped here, solving this issue requires bigger time commitment and
deeper Android app design understanding. Let's assume that you already started
your app and viewing something. Then you switch back to file manager and
trying to open another file in your app. Your app will fail with lack of
permissions for new URI. Looks like those permissions are given to first file
only, or maybe there is some other way to drop existing content resolver and
get new content resolver with proper permissions, or maybe it's just as simple
as refreshing the reference to cached Activity with new one. No idea, I had no
time to solve that issue, so I gave up.
At the moment, my main hope is that Qt guys will be able to incorporate
NativeActivity somehow, without the need of that big and scary java wrapper at
all, and we'll get some Qt-based class which will allow you to receive events
like start/restore/pause and so on instead of single entry point in form of
main method. Would gladly participate in any attempts to make it work through
NativeActivity, unfortunately my C++ background is not strong enough to mix
graphics layer of QML viewer with graphics layers of NativeActivity.
More information about the Interest