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. [SOLVED] Qt on Android : Open an external file
QtWS25 Last Chance

[SOLVED] Qt on Android : Open an external file

Scheduled Pinned Locked Moved Mobile and Embedded
9 Posts 4 Posters 11.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.
  • N Offline
    N Offline
    nologinma
    wrote on last edited by
    #1

    I'd like to open an external file from my app (for Android) but I have a big problem.

    The code is the following:

    @
    QString myfile;
    myfile = "/data/data/com.sample.org/sample.kml";
    myfile = QDir::toNativeSeparators(myfile);
    myfile = "file:///" + myfile;
    QDesktopServices::openUrl(QUrl(myfile));
    @

    If I run the same code in the desktop I haven't problem and the file is opened (in particolar mode Window ask me what application I have to associate to open the file; Google Earth in this case).

    The same example worked fine with Symbian, but with Android I have no response.

    Please can you help me ? There is a alternative way ?

    1 Reply Last reply
    0
    • J Offline
      J Offline
      jiangcaiyang
      wrote on last edited by
      #2

      I guess you add an extra slash '/', try delete one.
      Another proper way to test whether this url is correct is using QFileDialog to let user select one, then print the file path selected by the user.

      1 Reply Last reply
      0
      • N Offline
        N Offline
        nologinma
        wrote on last edited by
        #3

        I did several tests:

        @

        QString myfile;
        myfile = getXMLFileName();
        myfile = QDir::toNativeSeparators(myfile);
        myfile = "file:////" + myfile;
        QDesktopServices::openUrl(QUrl(myfile));
        @

        after also the following:

        @
        QString myfile;
        myfile = getXMLFileName();
        myfile = QDir::toNativeSeparators(myfile);
        myfile = "file://" + myfile;
        QDesktopServices::openUrl(QUrl(myfile));
        @

        and the following:

        @
        QString myfile;
        myfile = getXMLFileName();
        myfile = QDir::toNativeSeparators(myfile);
        QDesktopServices::openUrl(QUrl(myfile));
        @

        but none works.

        The Application output is the following:

        @D/Instrumentation(13081): checkStartActivityResult :Intent { act=android.intent.action.VIEW dat=/data/data/org.qtproject.gsp/files/gps.kml }
        D/Instrumentation(13081): checkStartActivityResult inent is instance of inent:
        W/System.err(13081): android.content.ActivityNotFoundException: No Activity found to handle Intent { act=android.intent.action.VIEW dat=/data/data/org.qtproject.gsp/files/gps.kml }
        W/System.err(13081): at android.app.Instrumentation.checkStartActivityResult(Instrumentation.java:1580)
        W/System.err(13081): at android.app.Instrumentation.execStartActivity(Instrumentation.java:1431)
        W/System.err(13081): at android.app.Activity.startActivityForResult(Activity.java:3428)
        W/System.err(13081): at android.app.Activity.startActivityForResult(Activity.java:3389)
        W/System.err(13081): at android.app.Activity.startActivity(Activity.java:3599)
        W/System.err(13081): at android.app.Activity.startActivity(Activity.java:3567)
        W/System.err(13081): at org.qtproject.qt5.android.QtNative.openURL(QtNative.java:116)
        W/System.err(13081): at dalvik.system.NativeStart.run(Native Method)
        W/IInputConnectionWrapper(13081): getSelectedText on inactive InputConnection
        W/IInputConnectionWrapper(13081): setComposingText on inactive InputConnection
        W/IInputConnectionWrapper(13081): getExtractedText on inactive InputConnection@

        I think it is a permissions issue.

        to resolve the problem above I have add the following block inside the AndroidMnifest:

        @<intent-filter>
        <action android:name="android.intent.action.VIEW"/>
        <category android:name="android.intent.category.DEFAULT"/>
        <data android:scheme="file">
        </intent-filter>@

        and now I haven't the error above but the file is not opened......(from google earth or another program).

        The message inside the Application output now is the following:

        @D/AbsListView( 9087): Get MotionRecognitionManager@

        but the application associate don't start...

        Where am I doing wrong?

        1 Reply Last reply
        0
        • C Offline
          C Offline
          cmazieri
          wrote on last edited by
          #4

          Perhaps, there is no application assigned to open this kind of file.

          QDesktopServices::openUrl() should use local system resources to open the file.

          Try to open more common files like: mp3 , html, pdf or text files.

          1 Reply Last reply
          0
          • N Offline
            N Offline
            nologinma
            wrote on last edited by
            #5

            I have change the directory where I create the kml file:

            from

            /data/data/org.qtproject.gsp/files/gps.kml

            to

            /storage/sdcard0/Documents/gps.kml

            and I have removed the code inside the AndroidManifest following:

            @<intent-filter>
            <action android:name="android.intent.action.VIEW"/>
            <category android:name="android.intent.category.DEFAULT"/>
            <data android:scheme="file">
            </intent-filter>@

            and now the Google Earth start.

            bq. Probably the initial directory was not accessible.

            1) Google Earth start but does not load the KML file even if it is well-formed

            2) An App in which directory should create a file? Is there a directory name in which to write the official files?

            1 Reply Last reply
            0
            • N Offline
              N Offline
              nologinma
              wrote on last edited by
              #6

              bq. 2) An App in which directory should create a file? Is there a directory name in which to write the official files?

              For the scond question, the answer is the following: (what I used)

              @QString path = QStandardPaths::writableLocation(QStandardPaths::GenericDataLocation);

              path.append("/gps");@

              bq. 1) Google Earth start but does not load the KML file even if it is well-formed

              for the moment I have no solution.

              My app open the Google Earth but not view the path inside the kml file.

              If I open the file without app from file browser the kml file is opend well.

              very strange

              1 Reply Last reply
              0
              • N Offline
                N Offline
                nologinma
                wrote on last edited by
                #7

                I have found an altenative solution.

                I can call a java class file from qt c++ and open the file using JNI method.

                The java class is the following:

                @//
                // Next72Utility.java
                //
                package org.qtproject.example.Chronometer;

                import android.app.Activity;
                import java.lang.Runnable;
                import android.content.Intent;
                import java.io.File;
                import android.net.Uri;

                public class Next72Utility extends org.qtproject.qt5.android.bindings.QtActivity
                {

                public static Next72Utility m_istance;

                public Next72Utility()
                {
                m_istance = this;
                }

                public static void openUrl(final String m_url, final String m_application_type)
                {
                m_istance.runOnUiThread(new Runnable() {
                public void run() {

                         Intent intent = new Intent();
                         intent.setAction(android.content.Intent.ACTION_VIEW);
                         File file = new File&#40;m_url&#41;;
                         intent.setDataAndType(Uri.fromFile(file&#41;, m_application_type&#41;;
                         m_istance.startActivity(intent);
                     }
                   });
                

                }

                }@

                and the call from c++ is the following:

                @QAndroidJniObject m_url = QAndroidJniObject::fromString("storage/sdcard0/gps/gps.kml");

                QAndroidJniObject m_application_type = QAndroidJniObject::fromString("application/vnd.google-earth.kml+xml");

                QAndroidJniObject::callStaticMethod<void>("org/qtproject/example/Chronometer/Next72Utility", "openUrl", "(Ljava/lang/String;Ljava/lang/String;)V",m_url.object<jstring>(),m_application_type.object<jstring>());@

                1 Reply Last reply
                0
                • E Offline
                  E Offline
                  EdOfTheMountain
                  wrote on last edited by
                  #8

                  I agree that it was a permissions problem.

                  I finally succeeded in opening a KML file in Google Earth without resorting to JNI.
                  The KML file must be located in external storage where Google Earth can access it.

                  @
                  // Saving KML file in QStandardPaths::DownloadLocation will work
                  QString destinationFolder = QStandardPaths::writableLocation(QStandardPaths::DownloadLocation);

                  // The KML file must be opened from a folder accessible to Google Earth app
                  // This will not work: /data/data/com.myccompany.myapp/
                  // This works........: /storage/emulated/0/Download/JOB-0001.kml
                  // /storage/sdcard0/Download/JOB-0001.kml
                  //
                  // Android: QStandardPaths::DownloadLocation

                  QUrl urlToLocalKmlFile =
                          QUrl::fromLocalFile&#40;kmlFileNamePath&#41;;
                  bool success = QDesktopServices::openUrl(urlToLocalKmlFile&#41;;
                  if( success&#41;
                  {
                      return;
                  }
                  
                  qCritical("openUrl failed?");
                  

                  @

                  1 Reply Last reply
                  0
                  • E Offline
                    E Offline
                    EdOfTheMountain
                    wrote on last edited by
                    #9
                    This post is deleted!
                    1 Reply Last reply
                    0

                    • Login

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