Ionic App - 从邮件附件中打开自定义类型的文件
Ionic App - Open a custom type of file from mail attachment
我正在开发一个小型 Ionic 应用程序,它需要以某种方式处理和显示一些数据。数据最初存储在 XLSX 格式的文件中,然后文件的扩展名更改为自定义扩展名,比方说 .myext
(对于那些想知道的人:我这样做所以所有 .xlsx
文件都不会自动使用我的应用程序打开,它需要特定的数据结构)。
正如您在 中看到的那样,我设法使我的应用能够打开 XLSX 文件,然后能够打开 .myext
文件。但是,当我通过 Android 设备上的文件系统资源管理器(在我的例子中是 File Commander)打开文件时,我只能用我的应用程序自动打开一个 .myext
文件。当用户在 his/her 邮件收件箱 (无论是专用应用程序还是浏览器)中收到此类文件时,我希望设备使用我的应用程序 自动打开 .myext
文件;当我尝试这样做时,我收到一条警告,说我的设备上没有能够打开此类文件的应用程序(文件已下载,我仍然可以通过设备文件系统资源管理器打开它)。
我试图在 AndroidManifest 中更改我的意图声明,但目前没有成功(请注意 android:scheme
行;我尝试了几种组合,使用一种、两种、三种或全部他们同时):
<activity android:configChanges="orientation|keyboardHidden|keyboard|screenSize|locale" android:label="@string/activity_name" android:launchMode="singleTop" android:name="MainActivity" android:theme="@android:style/Theme.Black.NoTitleBar" android:windowSoftInputMode="adjustResize">
<intent-filter android:label="@string/launcher_name">
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
<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" />
<data android:scheme="http" />
<data android:scheme="https" />
<data android:scheme="content" />
<data android:mimeType="*/*" />
<data android:pathPattern=".*\.myext" />
<data android:host="*" />
</intent-filter>
</activity>
我觉得我遗漏了什么,或者无法通过邮件附件打开自定义文件扩展名。你有什么想法吗?
在用户 e666 和 his/her 研究的帮助下,我设法修改了我的 Ionic 应用程序的意图声明,现在就像在我的 AndroidManifest.xml 中一样(说我的自定义文件扩展名是".myapp"):
<activity android:configChanges="orientation|keyboardHidden|keyboard|screenSize|locale" android:label="@string/activity_name" android:launchMode="singleTop" android:name="MainActivity" android:theme="@android:style/Theme.Black.NoTitleBar" android:windowSoftInputMode="adjustResize">
<intent-filter android:label="@string/launcher_name">
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
<intent-filter>
<data android:mimeType="application/vnd.myapp" android:pathPattern=".*\.myapp" android:scheme="content" />
<data android:mimeType="application/myapp" android:pathPattern=".*\.myapp" android:scheme="content" />
<data android:mimeType="application/octet-stream" android:pathPattern=".*\.myapp" android:scheme="content" />
<action android:name="android.intent.action.VIEW" />
<action android:name="android.intent.action.GET_CONTENT" />
<category android:name="android.intent.category.DEFAULT" />
<category android:name="android.intent.category.BROWSABLE" />
</intent-filter>
<intent-filter>
<data android:scheme="file" />
<data android:mimeType="*/*" />
<data android:pathPattern=".*\.myapp" />
<data android:host="*" />
<action android:name="android.intent.action.VIEW" />
<category android:name="android.intent.category.DEFAULT" />
<category android:name="android.intent.category.BROWSABLE" />
</intent-filter>
</activity>
第一个intent是我添加android平台支持时Ionic框架默认创建的intent。第二个意图是从邮件附件下载文件。第三个也是最后一个 Intent 过滤器用于通过用户 android 设备上的文件资源管理器打开的文件。
请注意,当您在应用程序的后端 Ionic Javascript 部分获取 URI 时,当您尝试通过邮件应用程序打开 .myapp
文件时,您会得到一个 content://
URI 或者邮件浏览器界面。但是,如果您尝试使用邮件 app/brower 接口(情况 1)直接打开文件,那么 content://
URI 的类型是不一样的,如果您下载了该文件,则尝试通过点击打开它"download complete" 通知(案例 2):
- 情况 1:您会得到类似
content://gmail-ls/myemail@gmail.com/messages/520/attachments/0.1/BEST/false
的内容
- 情况 2:您会得到类似
content://downloads/all_downloads/283
的内容
在第一种情况下,我找不到将此 URI 解析为 file:///
URI 的方法(我需要打开它然后读取文件中的数据)。
在第二种情况下,我在主题上安装了 cordova-plugin-filepath package (that creates the window.FilePath
object) and used it as follow to parse the content
URI into a file
URI (see 以了解如何将文件提供给您的应用程序):
window.plugins.webintent.getUri(function (url) { // success getUri
if(url.startsWith('content://')){
window.FilePath.resolveNativePath(url, function(res){
// parse content uri to file uri
var converted_url = "file://" + res;
// extract path and filename from parsed uri
var path = converted_url.slice(0, converted_url.lastIndexOf('/') + 1);
var filename = converted_url.substring(converted_url.lastIndexOf('/') + 1);
// read the file
$cordovaFile.readAsBinaryString(path, filename).then(function (result) { // success readAsBinaryString
// use the data
}, function(error){ //failure readAsBinaryString
// error when trying to open the file
});
}, function(error) { // failure resolveNativePath
// couldn't parse content URI to file URI
});
} else {
// the given URI is not a content URI
}
}, function(error) { // failure getUri
// no URI has been given to the app
}
我希望这对以后的其他人有所帮助。
window.plugins.webintent.getUri(function(url) {
if(url.startsWith('content://')) {
window.resolveLocalFileSystemURL(url, success, fail);
function success(fileEntry){
alert("present");
fileEntry.file(function(file){
alert("is")
var ft=new FileReader();
ft.onloadend=function(e){
// Do something
}
ft.readAsText(file);
});
}
如果它与 "ontent://gmail-ls/myemail@gmail.com/messages/520/attachments/0.1/BEST/false" 情况 1 一起使用。这有效
我正在开发一个小型 Ionic 应用程序,它需要以某种方式处理和显示一些数据。数据最初存储在 XLSX 格式的文件中,然后文件的扩展名更改为自定义扩展名,比方说 .myext
(对于那些想知道的人:我这样做所以所有 .xlsx
文件都不会自动使用我的应用程序打开,它需要特定的数据结构)。
正如您在 .myext
文件。但是,当我通过 Android 设备上的文件系统资源管理器(在我的例子中是 File Commander)打开文件时,我只能用我的应用程序自动打开一个 .myext
文件。当用户在 his/her 邮件收件箱 (无论是专用应用程序还是浏览器)中收到此类文件时,我希望设备使用我的应用程序 自动打开 .myext
文件;当我尝试这样做时,我收到一条警告,说我的设备上没有能够打开此类文件的应用程序(文件已下载,我仍然可以通过设备文件系统资源管理器打开它)。
我试图在 AndroidManifest 中更改我的意图声明,但目前没有成功(请注意 android:scheme
行;我尝试了几种组合,使用一种、两种、三种或全部他们同时):
<activity android:configChanges="orientation|keyboardHidden|keyboard|screenSize|locale" android:label="@string/activity_name" android:launchMode="singleTop" android:name="MainActivity" android:theme="@android:style/Theme.Black.NoTitleBar" android:windowSoftInputMode="adjustResize">
<intent-filter android:label="@string/launcher_name">
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
<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" />
<data android:scheme="http" />
<data android:scheme="https" />
<data android:scheme="content" />
<data android:mimeType="*/*" />
<data android:pathPattern=".*\.myext" />
<data android:host="*" />
</intent-filter>
</activity>
我觉得我遗漏了什么,或者无法通过邮件附件打开自定义文件扩展名。你有什么想法吗?
在用户 e666 和 his/her 研究的帮助下,我设法修改了我的 Ionic 应用程序的意图声明,现在就像在我的 AndroidManifest.xml 中一样(说我的自定义文件扩展名是".myapp"):
<activity android:configChanges="orientation|keyboardHidden|keyboard|screenSize|locale" android:label="@string/activity_name" android:launchMode="singleTop" android:name="MainActivity" android:theme="@android:style/Theme.Black.NoTitleBar" android:windowSoftInputMode="adjustResize">
<intent-filter android:label="@string/launcher_name">
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
<intent-filter>
<data android:mimeType="application/vnd.myapp" android:pathPattern=".*\.myapp" android:scheme="content" />
<data android:mimeType="application/myapp" android:pathPattern=".*\.myapp" android:scheme="content" />
<data android:mimeType="application/octet-stream" android:pathPattern=".*\.myapp" android:scheme="content" />
<action android:name="android.intent.action.VIEW" />
<action android:name="android.intent.action.GET_CONTENT" />
<category android:name="android.intent.category.DEFAULT" />
<category android:name="android.intent.category.BROWSABLE" />
</intent-filter>
<intent-filter>
<data android:scheme="file" />
<data android:mimeType="*/*" />
<data android:pathPattern=".*\.myapp" />
<data android:host="*" />
<action android:name="android.intent.action.VIEW" />
<category android:name="android.intent.category.DEFAULT" />
<category android:name="android.intent.category.BROWSABLE" />
</intent-filter>
</activity>
第一个intent是我添加android平台支持时Ionic框架默认创建的intent。第二个意图是从邮件附件下载文件。第三个也是最后一个 Intent 过滤器用于通过用户 android 设备上的文件资源管理器打开的文件。
请注意,当您在应用程序的后端 Ionic Javascript 部分获取 URI 时,当您尝试通过邮件应用程序打开 .myapp
文件时,您会得到一个 content://
URI 或者邮件浏览器界面。但是,如果您尝试使用邮件 app/brower 接口(情况 1)直接打开文件,那么 content://
URI 的类型是不一样的,如果您下载了该文件,则尝试通过点击打开它"download complete" 通知(案例 2):
- 情况 1:您会得到类似
content://gmail-ls/myemail@gmail.com/messages/520/attachments/0.1/BEST/false
的内容
- 情况 2:您会得到类似
content://downloads/all_downloads/283
的内容
在第一种情况下,我找不到将此 URI 解析为 file:///
URI 的方法(我需要打开它然后读取文件中的数据)。
在第二种情况下,我在主题上安装了 cordova-plugin-filepath package (that creates the window.FilePath
object) and used it as follow to parse the content
URI into a file
URI (see
window.plugins.webintent.getUri(function (url) { // success getUri
if(url.startsWith('content://')){
window.FilePath.resolveNativePath(url, function(res){
// parse content uri to file uri
var converted_url = "file://" + res;
// extract path and filename from parsed uri
var path = converted_url.slice(0, converted_url.lastIndexOf('/') + 1);
var filename = converted_url.substring(converted_url.lastIndexOf('/') + 1);
// read the file
$cordovaFile.readAsBinaryString(path, filename).then(function (result) { // success readAsBinaryString
// use the data
}, function(error){ //failure readAsBinaryString
// error when trying to open the file
});
}, function(error) { // failure resolveNativePath
// couldn't parse content URI to file URI
});
} else {
// the given URI is not a content URI
}
}, function(error) { // failure getUri
// no URI has been given to the app
}
我希望这对以后的其他人有所帮助。
window.plugins.webintent.getUri(function(url) {
if(url.startsWith('content://')) {
window.resolveLocalFileSystemURL(url, success, fail);
function success(fileEntry){
alert("present");
fileEntry.file(function(file){
alert("is")
var ft=new FileReader();
ft.onloadend=function(e){
// Do something
}
ft.readAsText(file);
});
}
如果它与 "ontent://gmail-ls/myemail@gmail.com/messages/520/attachments/0.1/BEST/false" 情况 1 一起使用。这有效