常见文件浏览器的意图过滤器

5
我将尽力为您翻译。这是与扩展名相关的应用程序关联问题。我已经阅读了文档一些问题,但主要的文件浏览器(如 ES 文件浏览器、Astro、File Manager by Rhythm Software)无法打开我的文件。
我的清单文件:
<activity android:name="com.comapping.android.map.MapActivity" android:configChanges="orientation|keyboardHidden">
    <intent-filter>
        <action android:name="android.intent.action.VIEW" />
        <category android:name="android.intent.category.BROWSABLE" />
        <category android:name="android.intent.category.DEFAULT" />
        <data android:scheme="file" android:host="*" android:pathPattern=".*\\.comap" android:mimeType="*/*" />
    </intent-filter>
    <intent-filter>
        <action android:name="android.intent.action.VIEW" />
        <category android:name="android.intent.category.BROWSABLE" />
        <category android:name="android.intent.category.DEFAULT" />
        <data android:scheme="content" android:host="*" android:pathPattern=".*\\.comap" android:mimeType="*/*" />
    </intent-filter>
    <intent-filter>
        <action android:name="android.intent.action.SEARCH" />
        <category android:name="android.intent.category.DEFAULT" />
    </intent-filter>
    <meta-data android:name="android.app.searchable"
        android:resource="@xml/searchable" />
</activity>

如果我使用下面的代码从我的应用程序中尝试打开文件,一切都正常(选择器不会显示):

String extension = "";
int dotIndex = downloadedFile.lastIndexOf('.');
if (dotIndex != -1) {
    extension = downloadedFile.substring(dotIndex + 1, downloadedFile.length());
}

// create an intent
Intent intent = new Intent(android.content.Intent.ACTION_VIEW);
Uri data = Uri.fromFile(new File(downloadedFile));
String type = MimeTypeMap.getSingleton().getMimeTypeFromExtension(extension);
if (type == null || type.length() == 0) {
    // if there is no acceptable mime type
    type = "application/octet-stream";
}
intent.setDataAndType(data, type);

// get the list of the activities which can open the file
List resolvers = context.getPackageManager().queryIntentActivities(intent, PackageManager.MATCH_DEFAULT_ONLY);
if (resolvers.isEmpty()) {
    (new AlertDialog.Builder(context)
            .setMessage(R.string.AttachmentUnknownFileType)
            .setNeutralButton(R.string.NeutralButtonText, null)
            .create()).show();
} else {
    context.startActivity(intent);
}

如果我尝试使用OpenIntents文件管理器打开它,一切正常-显示了长的选择器,有很多应用程序和我的应用程序在列表中。但是,如果我尝试使用ES文件浏览器或Rhythm文件浏览器打开它,则显示它们的自定义选择器(作为文本/图像/视频/音频打开)。这很糟糕。如果我尝试使用Astro打开它,则会打开OpenIntent的文件管理器。这也很糟糕。是否有解决问题的方法?在这种行为中,谁是错误的?感谢任何帮助。
1个回答

0

这里的问题之一是人们在Android MIME标识符中为解析为text/plain的文件包装共享意图的不同方式。

我的方法是找出我想要的文件的MIME类型。然后我将我的清单过滤器设置为这些类型。我使用两个过滤器。第一个只过滤我想要的MIME类型。第二个是第一个的副本加上方案。

然后,我通过像您所做的那样从应用程序发送共享来进行测试。当其中一个不起作用时,我使用toast查看我得到的STREAM,然后修改它以适应。例如,如果我得到一个OpenIntent STREAM,我必须删除

content://org.openintents.filemanager

这让我只剩下路径了。大多数情况下,我只需要移除:

file://

但这总是有点问题,因为整个内容/Uri/Intent方案都是一个自由混乱的集群。

稍后。我刚刚偶然发现了这个:

Intent intent = getIntent();                
String name = intent.getData().getPath();

也许那对你来说就够了。我很快会自己尝试一下。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接