简体   繁体   English

在下载/文档文件夹中为文件类型/扩展名注册 Android 应用程序

[英]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没有有用的文件扩展名,就像httphttps 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.

相关问题 注册一个 Android 应用程序以接收某些文件类型 - Registering an android app to receive certain file types 无法从 Android 应用程序的下载文件夹中读取文件 - Unable to read file from the Downloads folder for an android app Android (Java) - 打开存储在下载/文档中的文件 - Android (Java) - Open a file stored in Downloads/Documents 我无法从 android 的下载文件夹中获取文档 - I can't fetch documents from the downloads folder in android 如何将文件下载到Android中的内部下载文件夹中 - How to download file into internal downloads folder in Android 是否可以将文件放在 Android AVD 的“下载”文件夹中? - Is it possible to put a file in 'Downloads' folder in Android AVD? 无法从 Android 上的下载文件夹中读取文件 - Cannot read a file from Downloads folder on Android 第三方应用程序可以在“下载”文件夹中找到生成的文本文件,但未显示在Android的默认“下载”应用程序中 - Generated text file can be found by third party app in Download folder but not shown in Android's default Downloads app 使用 Android 10 (Q) 中的 SAF 文件选择器将文件从下载文件复制到本地应用程序文件夹 - Using SAF File Picker in Android 10 (Q) to copy files from Downloads to local app folder Android编写一个在“下载”应用程序中显示的文件 - Android write a file that shows in Downloads app
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM