[英]Registering Android App for File Types/Extensions in Downloads/Documents folder
I am currently trying to have my app opened when the user clicks a certain file in their file browser (eg .xyz
).我目前正在尝试在用户单击文件浏览器中的某个文件(例如
.xyz
)时打开我的应用程序。
The code I have for this in my manifest file is我在清单文件中的代码是
<intent-filter>
<action android:name="android.intent.action.VIEW" />
<category android:name="android.intent.category.DEFAULT" />
<category android:name="android.intent.category.BROWSABLE" />
<data android:scheme="file"
android:host="*" />
<data android:scheme="content"
android:host="*" />
<data android:mimeType="application/octet-stream" />
<data android:pathPattern=".*\\..*\\..*\\..*\\..*\\.xyz" />
<data android:pathPattern=".*\\..*\\..*\\..*\\.xyz" />
<data android:pathPattern=".*\\..*\\..*\\.xyz" />
<data android:pathPattern=".*\\..*\\.xyz" />
<data android:pathPattern=".*\\.xyz" />
<data android:pathPattern="*.xyz" />
</intent-filter>
This seems to work for files that are stored on the phone storage, eg in the Android/ folder, but not for files stored in the Documents and Downloads places.这似乎适用于存储在手机存储中的文件,例如 Android/ 文件夹中的文件,但不适用于存储在 Documents 和 Downloads 位置的文件。 That is because the uri of the file there is something like "
content://com.android.providers.downloads.documents/document/msf%3A45
", so the pathPattern does not work for it and the file browser says "Can't open file".那是因为文件的 uri 有类似“
content://com.android.providers.downloads.documents/document/msf%3A45
”的东西,所以 pathPattern 不适用于它并且文件浏览器显示“可以” t 打开文件”。
Is there any way to make opening the own app when the user clicks on files with the .xyz
extension work in these two locations?当用户在这两个位置单击扩展名为
.xyz
的文件时,有什么方法可以打开自己的应用程序? (I do not want the app to open any binary file, only the ones with the specified extension) (我不希望应用程序打开任何二进制文件,只打开具有指定扩展名的文件)
One alternative I thought about was using a custom mime type to save the files (eg application/my_xyz_type
) and to then change the line我想到的另一种选择是使用自定义 mime 类型来保存文件(例如
application/my_xyz_type
)然后更改行
<data android:mimeType="application/octet-stream" />
to至
<data android:mimeType="application/my_xyz_type" />
But when I do this using values.put(MediaStore.MediaColumns.MIME_TYPE, "application/my_xyz_type")
, the file gets saved as a binary file with application/octet-stream
as the mime type.但是当我使用
values.put(MediaStore.MediaColumns.MIME_TYPE, "application/my_xyz_type")
执行此操作时,该文件将保存为二进制文件,其中application/octet-stream
作为 mime 类型。 I am therefore assuming it is not possible to store files with a custom mime type.因此,我假设不可能存储具有自定义 mime 类型的文件。
"I am currently trying to have my app opened when the user clicks a certain file in their file browser (eg .xyz)
“我目前正在尝试在用户在文件浏览器中单击某个文件(例如 .xyz)时打开我的应用程序
That has never been a good option in Android for "custom" file extensions, and it is fairly unrealistic in modern versions.在 Android 中,对于“自定义”文件扩展名来说,这从来都不是一个好的选择,而且在现代版本中它是相当不现实的。
This seems to work for files that are stored on the phone storage, eg in the Android/ folder
这似乎适用于存储在手机存储中的文件,例如在 Android/ 文件夹中
Not necessarily.不必要。 The behavior will vary by OS version and "file browser" app.
行为会因操作系统版本和“文件浏览器”应用程序而异。
Is there any way to make opening the own app when the user clicks on files with the.xyz extension work in these two locations?
当用户在这两个位置单击扩展名为 .xyz 的文件时,有什么方法可以打开自己的应用程序?
No. More generally, you should assume that a content
Uri
does not have a useful file extension, just as http
and https
Uri
values often do not have a useful file extension.不。更一般地,您应该假设
content
Uri
没有有用的文件扩展名,就像http
和https
Uri
值通常没有有用的文件扩展名一样。
One alternative I thought about was using a custom mime type to save the files
我想到的一种替代方法是使用自定义 mime 类型来保存文件
Your <intent-filter>
is not limited to files that your app is involved in saving.您的
<intent-filter>
不限于您的应用程序参与保存的文件。
I am therefore assuming it is not possible to store files with a custom mime type
因此,我假设无法存储具有自定义 mime 类型的文件
That would not surprise me, though I have not tried it personally.这不会让我感到惊讶,尽管我没有亲自尝试过。
MediaStore
might check your supplied MIME type against a list of MIME types that it is willing to support and might swap in application/octet-stream
as a default if your MIME type is not on the list. MediaStore
可能会根据它愿意支持的 MIME 类型列表检查您提供的 MIME 类型,并且如果您的 MIME 类型不在列表中,则可能会默认交换application/octet-stream
。 This is another thing whose behavior might vary by OS version (and maybe by device manufacturer).这是另一件事,其行为可能因操作系统版本(也可能因设备制造商而异)而异。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.