【问题标题】:Ionic App - Open a custom type of file from mail attachmentIonic App - 从邮件附件中打开自定义类型的文件
【发布时间】:2017-08-09 02:34:29
【问题描述】:

我正在开发一个小型 Ionic 应用程序,它需要以某种方式处理和显示一些数据。数据最初存储在 XLSX 格式的文件中,然后将文件的扩展名更改为自定义扩展名,比如说 .myext(对于那些想知道的人:我这样做是因为我的应用程序不会自动打开所有 .xlsx 文件,需要一定的数据结构)。

正如您在a previous post I made about this app 中看到的那样,我设法让我的应用程序能够打开 XLSX 文件,然后能够打开 .myext 文件。虽然,我只能通过我的 Android 设备上的文件系统资源管理器(在我的情况下为 File Commander)打开我的应用程序自动打开一个 .myext 文件。我希望设备当用户在他/她的邮件收件箱中收到此类文件(无论是专用应用程序还是浏览器)时,使用我的应用程序自动打开.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>

我觉得我遗漏了一些东西,或者无法通过邮件附件打开自定义文件扩展名。你有什么想法吗?

【问题讨论】:

  • 您是否验证了您的最终 AndroidManifest 获得了您所拥有的价值?你也可以看看这个帖子:stackoverflow.com/questions/28198983/…
  • 是的,通过ionic run android 生成的AndroidManifest 考虑了我在我在myIonicApp\platforms\android\ 中修改的AndroidManifest 中声明的每个意图过滤器
  • 你看过这个答案stackoverflow.com/a/26393007/2549619 吗?好像用邮件附件有点不一样。
  • 几乎成功了。 Outlook(专用应用程序和浏览器内邮箱)不想打开文件,甚至不想下载该死的东西。 Gmail 成功尝试使用我的应用程序打开文件,但现在我面临另一个问题:提供的 URI 是content://gmail-ls/myemail@gmail.com/messages/520/attachments/0.1/BEST/false,我还不知道如何从中检索数据。你知道如何从这样的 URI 中检索文件/数据吗?如果我自己找到解决方案,我会保持更新。非常感谢您的帮助;-)
  • 我尝试使用这个:npmjs.com/package/cordova-plugin-filepath,但它抛出了以下错误:java.lang.Exception: Unable to resolve filesystem path for content://gmail-ls/myemail@gmail.com/messages/520/attachments/0.1/BEST/false,我猜这意味着该文件没有下载到设备文件系统上:-/跨度>

标签: android-intent ionic email-attachments


【解决方案1】:

在用户 e666 和他/她的研究的帮助下,我设法修改了我的 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。第二个意图是从邮件附件下载文件。第三个也是最后一个意图过滤器适用于通过用户 Android 设备上的文件资源管理器打开的文件。

请注意,当您在应用程序的后端 Ionic Javascript 部分中获取 URI 时,当您尝试通过邮件应用程序或邮件浏览器界面打开 .myapp 文件时,您会得到一个 content:// URI。但是content:// URI 的类型不一样,如果您尝试使用邮件应用程序/浏览器界面直接打开文件(案例 1),如果您下载文件 THEN 尝试通过点击“下载完成”打开它通知(案例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 包(它创建了window.FilePath 对象)并使用它来将content URI 解析为file URI(参见主题上的my previous post了解如何将文件提供给您的应用程序):

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
}

我希望这会在未来对其他人有所帮助。

【讨论】:

    【解决方案2】:
     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 .this works

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-05-24
      • 1970-01-01
      • 2016-07-13
      • 2016-02-15
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多