Open PDF on Android via QDesktopServices::openUrl?
-
I'm working on a Qt app whose target may be Windows desktop or Android. The app largely relies on a web-service that provides data in PDF and other formats. To view a PDF, I first write the data as a local PDF file (QDir::tempPath), then I call QDesktopServices::openUrl to show the file. This works nicely on Windows. On Android, I get the following error in the device's log:
@
D/Qt ( 5970): qrc:///PageDebugInfo.qml:551 (getReportCompleted): qml: getReportCompleted: url='file:////data/data/com.company.product/files/com.company.product.tmp.1409758061.pdf'
D/Instrumentation( 5970): checkStartActivityResult :Intent { act=android.intent.action.VIEW dat=file:////data/data/com.company.product/files/com.company.product.tmp.1409758061.pdf }
D/Instrumentation( 5970): checkStartActivityResult inent is instance of inent:
W/System.err( 5970): android.content.ActivityNotFoundException: No Activity found to handle Intent { act=android.intent.action.VIEW dat=file:////data/data/com.company.product/files/com.company.product.tmp.1409758061.pdf }
W/System.err( 5970): at android.app.Instrumentation.checkStartActivityResult(Instrumentation.java:1659)
W/System.err( 5970): at android.app.Instrumentation.execStartActivity(Instrumentation.java:1434)
W/System.err( 5970): at android.app.Activity.startActivityForResult(Activity.java:3430)
W/System.err( 5970): at android.app.Activity.startActivityForResult(Activity.java:3391)
W/System.err( 5970): at android.app.Activity.startActivity(Activity.java:3626)
W/System.err( 5970): at android.app.Activity.startActivity(Activity.java:3594)
W/System.err( 5970): at org.qtproject.qt5.android.QtNative.openURL(QtNative.java:116)
W/System.err( 5970): at dalvik.system.NativeStart.run(Native Method)
@I presume the reason is that I need to declare something in my Android manifest. I studied http://developer.android.com/guide/components/intents-filters.html but I do not seem to be able to figure it out.
I basically want to show the PDF in the device's default viewer. If there is more than one, the usual dialog to choose a viewer should be shown.
-
Take into account that external activity on Android (even the Android provided activities) cannot read temp files saved in data/data/com.company.product/
So, maybe the error come out because Android cannot read the file.You should write the pdf in an external public directory, like the directory returned by getExternalDir of Android API.
-
Thanks for the reply. I'm now using QStandardPaths::writableLocation (QStandardPaths::DownloadLocation) instead of QDir::tempPath. The path is now:
file:////storage/emulated/0/Download/com.company.product.tmp.1409818898.pdf
The error is similar:
@
checkStartActivityResult :Intent { act=android.intent.action.VIEW dat=file:////storage/emulated/0/Download/com.company.product.tmp.1409818898.pdf }
D/Instrumentation(21732): checkStartActivityResult inent is instance of inent:
W/System.err(21732): android.content.ActivityNotFoundException: No Activity found to handle Intent { act=android.intent.action.VIEW dat=file:////storage/emulated/0/Download/com.company.product.tmp.1409818898.pdf }
D/dalvikvm(21732): Rejecting registerization due to +iget-object-quick v7, v9, (#8)
[call stack follows]
@ -
Still not working...
-
I removed the extra slash in file:////.
-
I checked that the pdf file is correctly written in the Download folder and is a valid PDF; it can be opened manually by clicking on it in the Android app "My Files".
-