意图调用 gpx 应用程序

Intents to call gpx app

我的 Android 应用程序是一个帮助制图员绘制定向地图的工具。而不是使用 GPS 设备和小册子 he/she 使用单元格 phone 来记录这些 IOF 元素,然后保存为 .gpx 格式,可以由 CAD 软件读取绘制地图。

它没有图形界面。我的想法是使用一个已经可用的应用程序来绘制 gpx 文件中的轨迹。我用两个非常好的工具做了一些测试:GPXViewer 和 Locus Map。

当我触发下面的意图时,两者的反应相同,说我要显示的文件有问题,"TestGPX.gpx" 保存在单元格 phone 的文档目录中。在这两种情况下,我调用的应用程序都会打开但会抱怨,因为 Locus 更具体一些:"Invalid value: content://com.hbcavalcanti.com.isomspinner1.fileprovider/Documments/TestGPX.gpx".

因为它们是打开的,所以如果我手动选择文件,它们都可以毫无问题地绘制出来。我怀疑两者都抱怨不是偶然的,即我应该做一些不正确的事情:

    public void viewGPXVButton(View view){
    // Get directory Documents
    File root = android.os.Environment.getExternalStoragePublicDirectory
            (Environment.DIRECTORY_DOCUMENTS);
    // Form fileprovider
    String fileProvider = getPackageName()+".fileprovider";
    // Get complete file name. Use a fixed filename "TestGPX.gpx" just to make shure
    File file = new File(root , "TestGPX.gpx");
    // Get file URI
    try{
        Uri attachment = getUriForFile(getApplicationContext(), fileProvider, file);
        // Send it
        Intent intent = new Intent(Intent.ACTION_VIEW);
        intent.setDataAndType(attachment,"application/gpx");
        intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
        intent.setFlags(Intent.FLAG_ACTIVITY_NO_HISTORY);
        // Check the app is pressent
        if (intent.resolveActivity(getPackageManager())!= null){
            // Yes
            startActivity(intent);
        }else {
            // No
            Toast.makeText(getApplicationContext(),
                  "GPX viewer not installed", Toast.LENGTH_LONG ).show();
        }
    }
    // Fail to call
    catch (IllegalArgumentException e){
        Toast.makeText(getApplicationContext(),
                "Fail to open GPX viewer", Toast.LENGTH_LONG ).show();
    }
    finish();
}

Debug 是这么说的:

root:"/storage/emulated/0/Documents"
fileProvider:"com.hbcavalcanti.isomspinner1.fileprovider"
file:"/storage/emulated/0/Documents/TestGPX.gpx"
attachment:"content://com.hbcavalcanti.isomspinner1.fileprovider/Documents/TestGPX.gpx"

这是我的清单的一部分:

        <provider
        android:name="android.support.v4.content.FileProvider"
        android:authorities="com.hbcavalcanti.isomspinner1.fileprovider"
        android:grantUriPermissions="true"
        android:exported="false">
        <meta-data
            android:name="android.support.FILE_PROVIDER_PATHS"
            android:resource="@xml/filepaths" />
    </provider>

有什么想法吗?

https://github.com/k3b/LocationMapViewer/ is also capable to read gpx files in gpx1.0 and gpx1.1 format.

由于它是开源的,您可以调试发生的情况。您必须添加 "content: gpx" uri 来为您的用例清单以使其适用于非文件 uri-s。

de.k3b.android.LocationMapViewer#loadGeoPointDtosFromFile() 已经使用 contentResolver.openInputStream()

其他可能有用的工具:

所有讨论的 android 应用程序都可以在 f-droid app store

上免费获得

Locus Map 应用程序也有自己的 public API 供第 3 方开发人员使用。 勾选 this API class 允许将曲目直接发送到应用程序中,而无需在中间使用任何 GPX 文件。有了这个,您可以简单地在 Locus Map 地图上实时显示您的轨迹,而您的应用程序可以在后台运行。

我不确定这是否是您想要实现的用例,或者积分是否真的 "only" 在移动设备上显示 GPX 轨迹 phone。如果第二个是正确的,也许更有用的可能是将轨迹导出到 KML 文件中,Google 地球也支持此功能,也许(不确定)也直接由 Google 地图应用程序支持。