我正在尝试将我的应用程序与扩展相关联。我读过documentation和 some questions关于这个问题。但是主要的文件浏览器(如es文件浏览器、astro、Rhythm软件的文件管理器)无法打开我的文件。
我的 list
文件:
<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 的文件管理器。这也很糟糕。
这个问题有什么解决办法吗?这种行为谁错了?
感谢您的帮助。
最佳答案
这里的部分问题是人们包装共享 Intent 的所有不同方式,例如,在 Android mime 标识符中解析为 text/plain 的文件。
我的方法是找出我想要的文件是什么 mime 类型。然后我将我的 list 过滤器设置为这些类型。我使用两个过滤器。第一个只是过滤我想要的 mime 。第二个是第一个加方案的副本。
然后我会像您一样通过从应用发送分享来进行测试。当一个不工作时,我用 toast 来查看我为 STREAM 得到了什么,然后改变它以适应。例如,如果我得到一个 OpenIntent STREAM,我必须删除
content://org.openintents.filemanager
这给我留下了路径。大多数情况下,我只需要删除:
file://
但它总是有些东西,因为整个 content/Uri/Intent 方案是一个免费的集群 whatsit。
稍后。我只是偶然发现了这个:
Intent intent = getIntent();
String name = intent.getData().getPath();
也许这对你有用。我很快就会自己尝试一下。
关于android - 常见文件浏览器的 Intent 过滤器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10979939/