如何告诉 Android 在我的某些设备上的应用程序中打开自定义文件类型,例如三星
How to tell Android to open custom file type in my application on some devices, e.g. Samsung
我读过几个类似的问题
(Android intent filter for a particular file extension?,
Creating app which opens a custom file extension, Associating App with Epub format),但我的问题略有不同。对不起,带图的长post,不过还是明白我的意思就好了。
我想从文件资源管理器等应用程序打开自定义文件类型。我的意图过滤器看起来像:
[IntentFilter(new[] { Intent.ActionView, Intent.ActionSend, Intent.ActionEdit },
Categories = new[] { Intent.CategoryDefault },
DataMimeType = "*/*",
DataHost = "*",
DataPathPattern = ".*\.pdo",
Icon = "@drawable/icon"
)]
当我在 Total Commander 中点击文件时,出现下一个弹出窗口 window:
我可以 select“打开方式”或“打开方式”,然后在 window select 我的应用程序中。一切正常。
但是当我在文件资源管理器 (Samsung A51) 中点击文件时,出现了下一个弹出窗口 window:
所以第一个问题是我错过了什么以及如何告诉文件资源管理器用我的应用程序打开这个文件。
当我点击 txt 文件时,出现下一个 window:
如果我的应用程序显示在这个漂亮的 window 中会是更好的选择 :) 所以第二个问题是如何实现此行为。提前谢谢你。
编辑
这里是生成的AndroidManifest.xml
的相关部分
<manifest>
<application
android:name="android.app.Application"
android:debuggable="true"
android:appComponentFactory="androidx.core.app.CoreComponentFactory"
android:requestLegacyExternalStorage="true">
<activity
android:theme="@2131689703"
android:icon="@2131165558"
android:name="MainActivity"
android:launchMode="2"
android:configChanges="0x26c0"
android:alwaysRetainTaskState="true">
<intent-filter android:icon="@2131165558">
<action android:name="android.intent.action.VIEW" />
<action android:name="android.intent.action.SEND" />
<action android:name="android.intent.action.EDIT" />
<category android:name="android.intent.category.DEFAULT" />
<data android:host="*" />
<data android:pathPattern=".*.pdo" />
<data android:mimeType="application/pdo" />
<data android:mimeType="application/octet-stream" />
</intent-filter>
</activity>
<activity
android:theme="@2131689957"
android:icon="@2131165558"
android:name="SplashActivity"
android:configChanges="0x480"
android:noHistory="true">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<provider
android:name="xamarin.essentials.fileProvider"
android:exported="false"
android:authorities="de.novel.loadsol.fileProvider"
android:grantUriPermissions="true">
<meta-data
android:name="android.support.FILE_PROVIDER_PATHS"
android:resource="@2131820550" />
</provider>
</application>
</manifest>
编辑 2
看起来问题是某些设备特有的。在 Vivo 和 Honor 手机上,文件资源管理器可以很好地打开我的应用程序。
我之前想要在我的应用程序中为 .smbf
文件扩展名提供类似的行为。这些是我达到的最终设置。
[IntentFilter(
actions: new string[] { Intent.ActionView },
Categories = new string[] { Intent.CategoryDefault, Intent.CategoryBrowsable },
DataPathPatterns = new[] { @".*\.smbf", @".*\.smbf" },
DataMimeTypes = new[] { "*/*", "*/*" },
DataHosts = new[] { "*", "*" },
DataSchemes = new[] { "content", "file" })]
在花了一整天之后,我想出了一个适用于 Samsung File Explorer 的选项。所以我又添加了一个意图过滤器:
[IntentFilter(new[] { Intent.ActionView, Intent.ActionSend, Intent.ActionEdit,
Intent.ActionGetContent, Intent.ActionSendto },
Categories = new[] { Intent.CategoryDefault,
Intent.CategoryBrowsable, Intent.CategoryOpenable },
Icon = "@drawable/icon",
DataMimeType = "*/*",
DataScheme = "content"
)]
这种方法的主要缺点很明显 - 我的应用程序建议从资源管理器打开 any 文件。我尝试了不同的 mime 类型,例如 application/octet-stream
、application/binary
、application/pdo
,但它们都不起作用。当然,我在app里检查了一个文件类型,'foreign'个文件打不开,但是这个问题还是很烦人。
我仍然想知道为什么 content
是与某些文件浏览器交互的单一方式(我只在三星设备上发现了这种行为,但我想这也可能发生在其他一些设备上)。
我读过几个类似的问题 (Android intent filter for a particular file extension?, Creating app which opens a custom file extension, Associating App with Epub format),但我的问题略有不同。对不起,带图的长post,不过还是明白我的意思就好了。
我想从文件资源管理器等应用程序打开自定义文件类型。我的意图过滤器看起来像:
[IntentFilter(new[] { Intent.ActionView, Intent.ActionSend, Intent.ActionEdit },
Categories = new[] { Intent.CategoryDefault },
DataMimeType = "*/*",
DataHost = "*",
DataPathPattern = ".*\.pdo",
Icon = "@drawable/icon"
)]
当我在 Total Commander 中点击文件时,出现下一个弹出窗口 window:
我可以 select“打开方式”或“打开方式”,然后在 window select 我的应用程序中。一切正常。
但是当我在文件资源管理器 (Samsung A51) 中点击文件时,出现了下一个弹出窗口 window:
所以第一个问题是我错过了什么以及如何告诉文件资源管理器用我的应用程序打开这个文件。
当我点击 txt 文件时,出现下一个 window:
如果我的应用程序显示在这个漂亮的 window 中会是更好的选择 :) 所以第二个问题是如何实现此行为。提前谢谢你。
编辑
这里是生成的AndroidManifest.xml
的相关部分 <manifest>
<application
android:name="android.app.Application"
android:debuggable="true"
android:appComponentFactory="androidx.core.app.CoreComponentFactory"
android:requestLegacyExternalStorage="true">
<activity
android:theme="@2131689703"
android:icon="@2131165558"
android:name="MainActivity"
android:launchMode="2"
android:configChanges="0x26c0"
android:alwaysRetainTaskState="true">
<intent-filter android:icon="@2131165558">
<action android:name="android.intent.action.VIEW" />
<action android:name="android.intent.action.SEND" />
<action android:name="android.intent.action.EDIT" />
<category android:name="android.intent.category.DEFAULT" />
<data android:host="*" />
<data android:pathPattern=".*.pdo" />
<data android:mimeType="application/pdo" />
<data android:mimeType="application/octet-stream" />
</intent-filter>
</activity>
<activity
android:theme="@2131689957"
android:icon="@2131165558"
android:name="SplashActivity"
android:configChanges="0x480"
android:noHistory="true">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<provider
android:name="xamarin.essentials.fileProvider"
android:exported="false"
android:authorities="de.novel.loadsol.fileProvider"
android:grantUriPermissions="true">
<meta-data
android:name="android.support.FILE_PROVIDER_PATHS"
android:resource="@2131820550" />
</provider>
</application>
</manifest>
编辑 2
看起来问题是某些设备特有的。在 Vivo 和 Honor 手机上,文件资源管理器可以很好地打开我的应用程序。
我之前想要在我的应用程序中为 .smbf
文件扩展名提供类似的行为。这些是我达到的最终设置。
[IntentFilter(
actions: new string[] { Intent.ActionView },
Categories = new string[] { Intent.CategoryDefault, Intent.CategoryBrowsable },
DataPathPatterns = new[] { @".*\.smbf", @".*\.smbf" },
DataMimeTypes = new[] { "*/*", "*/*" },
DataHosts = new[] { "*", "*" },
DataSchemes = new[] { "content", "file" })]
在花了一整天之后,我想出了一个适用于 Samsung File Explorer 的选项。所以我又添加了一个意图过滤器:
[IntentFilter(new[] { Intent.ActionView, Intent.ActionSend, Intent.ActionEdit,
Intent.ActionGetContent, Intent.ActionSendto },
Categories = new[] { Intent.CategoryDefault,
Intent.CategoryBrowsable, Intent.CategoryOpenable },
Icon = "@drawable/icon",
DataMimeType = "*/*",
DataScheme = "content"
)]
这种方法的主要缺点很明显 - 我的应用程序建议从资源管理器打开 any 文件。我尝试了不同的 mime 类型,例如 application/octet-stream
、application/binary
、application/pdo
,但它们都不起作用。当然,我在app里检查了一个文件类型,'foreign'个文件打不开,但是这个问题还是很烦人。
我仍然想知道为什么 content
是与某些文件浏览器交互的单一方式(我只在三星设备上发现了这种行为,但我想这也可能发生在其他一些设备上)。