Android Question [SOLVED] Start Google Earth with KML file

sea121

Member
Licensed User
Longtime User
Hello everyone,

I have a big problem with google earth, i can't pass him a kml file, i followed all the posts here but i always get the same error .

1) the kml file is correct, google earth can open it.
2) i am using android:targetSdkVersion="24"

can someone kindly show me a small working demo?

Many Thanks.
 

Attachments

  • error.jpg
    error.jpg
    14.8 KB · Views: 211
Solution
This is how I openAlpineQuest or Google Earth with a KML file. I target SDK 28 and PERMISSION_WRITE_EXTERNAL_STORAGE to write to the Download folder as my app doesn't go in the PLay Store. You may need to change to write the KML to File.DirInternal
Manifest entries:
AddApplicationText(
  <provider
  android:name="android.support.v4.content.FileProvider"
  android:authorities="$PACKAGE$.provider"
  android:exported="false"
  android:grantUriPermissions="true">
  <meta-data
      android:name="android.support.FILE_PROVIDER_PATHS"
      android:resource="@xml/provider_paths"/>
  </provider>
)
CreateResource(xml, provider_paths,
    <paths xmlns:android="http://schemas.android.com/apk/res/android">
    <external-path name="root" path="" />
</paths>
)
...

agraham

Expert
Licensed User
Longtime User
This is how I openAlpineQuest or Google Earth with a KML file. I target SDK 28 and PERMISSION_WRITE_EXTERNAL_STORAGE to write to the Download folder as my app doesn't go in the PLay Store. You may need to change to write the KML to File.DirInternal
Manifest entries:
AddApplicationText(
  <provider
  android:name="android.support.v4.content.FileProvider"
  android:authorities="$PACKAGE$.provider"
  android:exported="false"
  android:grantUriPermissions="true">
  <meta-data
      android:name="android.support.FILE_PROVIDER_PATHS"
      android:resource="@xml/provider_paths"/>
  </provider>
)
CreateResource(xml, provider_paths,
    <paths xmlns:android="http://schemas.android.com/apk/res/android">
    <external-path name="root" path="" />
</paths>
)
B4X:
Sub Activity_Create(FirstTime As Boolean)
    ' ...
    Dim rp As RuntimePermissions
    rp.CheckAndRequest(rp.PERMISSION_WRITE_EXTERNAL_STORAGE) ' Implicit read capability if granted
    Wait For Activity_PermissionResult (Permission As String, Result As Boolean)
    Log($"PERMISSION_WRITE_EXTERNAL_STORAGE = ${Result}"$)
   ' ...
End Sub


Sub btnCreateKML_Click
    Wait For(CheckNoResult) Complete (NoResult As Boolean)
    If NoResult Then
        EnableUI
        Return
    End If
    Dim path As String = File.DirRootExternal & "/Download"
    Dim KMLname As String = ResultName
    Dim index As Int = KMLname.IndexOf("/")
    If index > 0 Then KMLname = KMLname.SubString2(0, index)
    Dim KMLgridRef As String = ResultGridRef
    Dim wgs84longitude As String = ResultLongitudeWGS84
    Dim wgs84latitude As String = ResultLatitudeWGS84
   
    Dim kml As String = "<?xml version='1.0' encoding='UTF-8'?>" & CRLF
    kml = kml & "<kml xmlns='http://www.opengis.net/kml/2.2'>" & CRLF
    kml = kml & "  <Document>" & CRLF
    kml = kml & "    <name>" & KMLname & "</name>" & CRLF
    kml = kml & "    <Placemark>" & CRLF
    kml = kml & "      <name>" & KMLname & "</name>" & CRLF
    kml = kml & "      <description>" & KMLgridRef & "</description>" & CRLF
    kml = kml & "      <Point>" & CRLF
    kml = kml & "        <coordinates>" & wgs84longitude & "," & wgs84latitude & ",0 </coordinates>" & CRLF
    kml = kml &  "      </Point>" & CRLF
    kml = kml & "    </Placemark>" & CRLF
    kml = kml & "  </Document>" & CRLF
    kml = kml & "</kml>"
    ' AlpineQuest doesn't seem to like spaces in filenames when opened by FileProvider and throws a permission exception
    Dim Filename As String = KMLname.Replace(" ", "_") & ".kml"
    File.WriteString(path, Filename, kml)

    Provider.SharedFolder = File.DirRootExternal
    Dim intent1 As Intent
    intent1.Initialize(intent1.ACTION_VIEW, Provider.GetFileUri("Download/" & Filename))
    intent1.SetType("application/vnd.google-earth.kml+xml")
    intent1.Flags = 1 'FLAG_GRANT_READ_URI_PERMISSION
    StartActivity(intent1)
End Sub
 
Upvote 1
Solution

sea121

Member
Licensed User
Longtime User
Thank You Agraham for your reply,

i modified your code to my sub :

B4X:
Sub Start_Street(modo As Int, coord As LatLng)
    
    'https://moz.com/blog/everything-you-never-wanted-to-know-about-google-maps-parameters
    
    Dim Intent1 As Intent
    Dim Uri As String
    
    Select Case modo
        Case 1
            Uri=$"google.streetview:cbll=$2.5{coord.Latitude},$2.5{coord.Longitude}"$
            Intent1.Initialize(Intent1.ACTION_VIEW, Uri)
            
        Case 0
            Uri= $"geo:$2.5{coord.Latitude},$2.5{coord.Longitude}"$
            Intent1.Initialize(Intent1.ACTION_VIEW, Uri )
            Intent1.SetComponent("com.google.earth/.EarthActivity")
            
        Case 2
            
            Dim path As String = File.DirRootExternal & "/Download"
            Dim filename As String  = sonda.serialnum.trim
            If filename = "" Then filename = "POINT"
            
            Dim kml As String = "<kml>" & CRLF
            kml = kml & "    <Placemark>" & CRLF
            kml = kml & "      <name>" & filename & "</name>" & CRLF
            kml = kml & "      <description>" & filename & "</description>" & CRLF
            kml = kml & "      <Point>" & CRLF
            kml = kml & "        <coordinates>" & coord.Longitude & "," & coord.Latitude & ",0 </coordinates>" & CRLF
            kml = kml &  "      </Point>" & CRLF
            kml = kml & "    </Placemark>" & CRLF
            kml = kml & "</kml>" & CRLF

            filename = filename.Trim & ".kml"
            File.WriteString(path, filename, kml)

            Provider.SharedFolder = File.DirRootExternal
            Intent1.Initialize(Intent1.ACTION_VIEW, Provider.GetFileUri("Download/" & filename))
            Intent1.SetType("application/vnd.google-earth.kml+xml")
            Intent1.Flags = 1 'FLAG_GRANT_READ_URI_PERMISSION

    End Select

    StartActivity(Intent1)

End Sub

this is the error that i see in log :

--------- beginning of system
** Activity (main) Pause, UserClosed = true **
*** Service (starter) Create ***
** Service (starter) Start **
** Activity (main) Create, isFirst = true **
Using FileProvider? true
** Activity (main) Resume **
PERMISSION_WRITE_EXTERNAL_STORAGE = true
*** Service (starter) Create ***
** Service (starter) Start **
** Activity (main) Create, isFirst = true **
Using FileProvider? true
** Activity (main) Resume **
PERMISSION_WRITE_EXTERNAL_STORAGE = true
Error occurred on line: 34 (FileProvider)
java.lang.reflect.InvocationTargetException
at java.lang.reflect.Method.invoke(Native Method)
at anywheresoftware.b4j.object.JavaObject.RunMethod(JavaObject.java:132)
at com.Domox.Tracker.fileprovider._getfileuri(fileprovider.java:120)
at com.Domox.Tracker.main._start_street(main.java:4673)
at com.Domox.Tracker.main$ResumableSub_Label12_LongClick.resume(main.java:4576)
at anywheresoftware.b4a.shell.DebugResumableSub$DelegatableResumableSub.resumeAsUserSub(DebugResumableSub.java:48)
at java.lang.reflect.Method.invoke(Native Method)
at anywheresoftware.b4a.shell.Shell.runMethod(Shell.java:732)
at anywheresoftware.b4a.shell.Shell.raiseEventImpl(Shell.java:348)
at anywheresoftware.b4a.shell.Shell.raiseEvent(Shell.java:255)
at java.lang.reflect.Method.invoke(Native Method)
at anywheresoftware.b4a.ShellBA.raiseEvent2(ShellBA.java:144)
at anywheresoftware.b4a.BA.raiseEvent2(BA.java:197)
at anywheresoftware.b4a.BA.raiseEvent(BA.java:193)
at anywheresoftware.b4a.shell.DebugResumableSub$DelegatableResumableSub.resume(DebugResumableSub.java:43)
at anywheresoftware.b4a.keywords.Common$13.run(Common.java:1714)
at android.os.Handler.handleCallback(Handler.java:873)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:214)
at android.app.ActivityThread.main(ActivityThread.java:7050)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:494)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:965)
Caused by: java.lang.IllegalArgumentException: Failed to find configured root that contains /storage/emulated/0/Download/POINT.kml
at androidx.core.content.FileProvider$SimplePathStrategy.getUriForFile(FileProvider.java:744)
at androidx.core.content.FileProvider.getUriForFile(FileProvider.java:418)
... 23 more
 
Upvote 0
Top