Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. Mobile and Embedded
  4. Qt & Android, Launching 3rdParty PDF-Viewer

Qt & Android, Launching 3rdParty PDF-Viewer

Scheduled Pinned Locked Moved Solved Mobile and Embedded
androidpdfjavadesktopservice
4 Posts 2 Posters 1.6k Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • J Offline
    J Offline
    J.Hilk
    Moderators
    wrote on 26 Mar 2019, 10:34 last edited by J.Hilk
    #1

    Hi everyone,

    I'm creating a pdf out of data. That works fine, and if the user clicks on the generated pdf, the installed pdfviewer app is started and everything is displayed correctly.

    Now I'm trying to launch that pdf viewer from my QtApplication.

    I tried 2 approaches QDesktopServices and Writing my own Activity extension.

    //DesktopService
    
    QString filePath = QStandardPaths::writableLocation(QStandardPaths::DownloadLocation) + QString("/Report.pdf"); //The file exists at that location, I used the same path to create the pdf
    QDesktopServices::openUrl(QUrl::fromLocalFile(filePath));
    
    //Java approach
    //cpp call
    QAndroidJniObject javaFilenameStr = QAndroidJniObject::fromString(filePath);
    QAndroidJniObject::callStaticMethod<void>("myApp/pdfView/PdfViewerChoice","showPdf","(Ljava/lang/String;)V", javaFilenameStr.object<jstring>());
    
    //Java class
    package myApp.pdfView;
    
    import java.io.File;
    import android.app.Activity;
    import android.content.Intent;
    import android.util.Log;
    import android.net.Uri;
    
    public class PdfViewerChoice extends org.qtproject.qt5.android.bindings.QtActivity {
        private static PdfViewerChoice m_pdfViewer;
    
        public PdfViewerChoice()
        {
            Log.d("PdfViewerChoice::PdfViewerChoice", "Constructor");
            m_pdfViewer = this;
        }
    
        public static void showPdf(String filepath)
        {
            Log.d("PdfViewerChoice::PdfViewerChoice", "showPdf");
            Log.d("PdfViewerChoice::PdfViewerChoice", filepath);
            //String f = "file://" + filepath;
            //Uri uri = Uri.parse(f);
    
            File file = null;
                 file = new File(filepath);
    
            if(file.exists())
                Log.d("File Exists", "true");
            else
                Log.d("File Exists", "false");
    
            Intent target = new Intent(Intent.ACTION_VIEW);
            target.setDataAndType(Uri.fromFile(file), "application/pdf");
            target.setFlags(Intent.FLAG_ACTIVITY_NO_HISTORY);
    
            Intent intent = Intent.createChooser(target, "Open File");
            try {
                m_pdfViewer.getApplicationContext().startActivity(intent);
            } catch (android.content.ActivityNotFoundException ex){
                Log.e("PdfViewerChoice", "catched android.content.ActivityNotFoundException while starting activity");
                ex.printStackTrace();
            }
        }
    }
    
    

    I even get a file exists true log from the java class. But no chooser or pdf viewer is launched.

    For reasons I can't go deeper into I'm limited to Qt 5.11.3, this is only supposed to work on android and on PC and Mac. PC/Mac works fine with the DesktopService call, but Android refuses to work.

    What am I missing, perhaps a special android permission ?


    Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct


    Q: What's that?
    A: It's blue light.
    Q: What does it do?
    A: It turns blue.

    1 Reply Last reply
    0
    • V Offline
      V Offline
      VRonin
      wrote on 26 Mar 2019, 11:35 last edited by
      #2

      Welcome to hell.
      @ekkescorner wrote an excellent blog post to explain this: https://blog.qt.io/blog/2017/12/01/sharing-files-android-ios-qt-app/

      "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
      ~Napoleon Bonaparte

      On a crusade to banish setIndexWidget() from the holy land of Qt

      J 2 Replies Last reply 26 Mar 2019, 11:51
      3
      • V VRonin
        26 Mar 2019, 11:35

        Welcome to hell.
        @ekkescorner wrote an excellent blog post to explain this: https://blog.qt.io/blog/2017/12/01/sharing-files-android-ios-qt-app/

        J Offline
        J Offline
        J.Hilk
        Moderators
        wrote on 26 Mar 2019, 11:51 last edited by
        #3

        @VRonin thanks for the link, I'll read through it.

        I have a slightly different class in an other project to attach a pdf to the E-Mail program that is working fine. So I'm assuming it's something minor right now x)

                    Intent intent = new Intent(Intent.ACTION_SEND);
                    intent.setType("message/rfc822");
        
                    intent.putExtra(Intent.EXTRA_SUBJECT, "AttachmentTest");
                    intent.putExtra(Intent.EXTRA_EMAIL, new String[]{sendTo});
                    intent.putExtra(Intent.EXTRA_STREAM, uri);
                    Intent mailer = Intent.createChooser(intent, null);
                    if(mailer == null)
                    {
                        Log.e("Mailer::sendMail", "Couldn't get Mail Intent");
                        return;
                    }
        
                    m_instance.getApplicationContext().startActivity(mailer);
        

        Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct


        Q: What's that?
        A: It's blue light.
        Q: What does it do?
        A: It turns blue.

        1 Reply Last reply
        0
        • V VRonin
          26 Mar 2019, 11:35

          Welcome to hell.
          @ekkescorner wrote an excellent blog post to explain this: https://blog.qt.io/blog/2017/12/01/sharing-files-android-ios-qt-app/

          J Offline
          J Offline
          J.Hilk
          Moderators
          wrote on 10 Apr 2019, 15:00 last edited by
          #4

          Turns out, with an android target of 23 or older my approach is more than fine. But google decided to make it more complicated going forwards. As 23 is no longer viable version for updates.

          So, I read through the block post (multiple times), extracted everything I needed and managed to cram everything into one java file.

          also I get this warning:

          WARNING: Configuration 'compile' is obsolete and has been replaced with 'implementation' and 'api'.
          It will be removed at the end of 2018. For more information see: http://d.android.com/r/tools/update-dependency-configurations.html

          since it's now April 2019 I'm going to ignore that until it bites me in the back ;-)

          All in all I'm once again reminded why I'm not a fan of Java....

          Thanks @VRonin and @ekkescorner , for helping me solve this, very much appreciated.


          Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct


          Q: What's that?
          A: It's blue light.
          Q: What does it do?
          A: It turns blue.

          1 Reply Last reply
          1

          • Login

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • Users
          • Groups
          • Search
          • Get Qt Extensions
          • Unsolved