0

I'm developing a small Ionic app that needs some data to be processed and displayed in a certain way. The data are originally stored in a file under XLSX format, then the file's extension is changed to a custom one, let's say .myext (for those who wonder: I do that so all .xlsx files are not automatically opened with my app, which needs a certain data structuration).

I managed, like you can see in a previous post I made about this app, to make my app able to open XLSX files, and then able to open .myext files. Though, I can only open a .myext file with my app automatically when opening it via a file system explorer on my Android device (in my case File Commander). I want the device to automatically open .myext files with my app when the user receive such file in his/her mail inbox (whether dedicated app or browser); when I try to do so, I got a warning saying that no app capable to open such files is available on my device (the file is downloaded anyway and I still can open it via device file system explorer).

I tried to change my intent declaration in the AndroidManifest, without any luck for now (note the android:scheme lines; I tried several combinations, using one, two, three, or all of them at the same time):

<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>

I feel like I'm missing something, or that is not possible to open custom file extension via mail attachment. Do you have any idea to do so?

Community
  • 1
  • 1
TheFrenchieCake
  • 123
  • 1
  • 12
  • Have you verified that your final AndroidManifest get the value that you have ? You can also take a look at this post : http://stackoverflow.com/questions/28198983/ionic-cordova-add-intent-filter-using-config-xml – e666 Nov 30 '15 at 16:50
  • Yes, the AndroidManifest that is generated through `ionic run android` takes in account every intent-filters I declared in the AndroidManifest I modified in myIonicApp\platforms\android\ – TheFrenchieCake Dec 01 '15 at 09:35
  • And have you look at this answer http://stackoverflow.com/a/26393007/2549619 ? It seems that with email attachement it's a bit different. – e666 Dec 01 '15 at 12:43
  • Well it almost worked. Outlook (both dedicated app and in-browser mailbox) doesn't want to open the file, nor even to download the damn thing. Gmail successfully tried to open the file with my app, but now I face another problem: the provided URI is `content://gmail-ls/myemail@gmail.com/messages/520/attachments/0.1/BEST/false` and I don't know how to retrieve data from it yet. Do you have an idea on how to retrieve the file/data from such an URI? I'll keep the post updated if I find a solution myself. Many thanks for the help already ;-) – TheFrenchieCake Dec 01 '15 at 14:19
  • I tried to use this: https://www.npmjs.com/package/cordova-plugin-filepath but it throws me the following error : `java.lang.Exception: Unable to resolve filesystem path for content://gmail-ls/myemail@gmail.com/messages/520/attachments/0.1/BEST/false` which would mean, I guess, that the file is not downloaded on the device filesystem :-/ – TheFrenchieCake Dec 01 '15 at 14:49

2 Answers2

1

With the help of user e666 and his/her research, I managed to modify the intents declaration of my Ionic app, which are now like follow in my AndroidManifest.xml (saying my custom file extension is ".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>

The first intent is the default one created by the Ionic framework when I added the support of the android platform. The second intent is for file downloaded from a mail attachment. The third and last intent filter is for file opened through a file explorer on the user's android device.

Note that when you fetch the URI in backend Ionic Javascript part of the app, you get a content:// URI when you try to open a .myapp file via mail app or mail browser interface. But the type of content:// URI is not the same if you try to directly open the file with the mail app/brower interface (case 1), and if you download the file THEN try to open it by taping the "download complete" notification (case 2):

  • case 1: you'll get something like content://gmail-ls/myemail@gmail.com/messages/520/attachments/0.1/BEST/false
  • case 2: you'll get something like content://downloads/all_downloads/283

In the first case, I couldn't find a way to parse this URI to a file:/// URI (which I need to open then read the data in the file).

In the second case, I installed the 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 my previous post on the subject to understand how to feed a file to your app):

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
}

I hope this will help someone else in the future.

Community
  • 1
  • 1
TheFrenchieCake
  • 123
  • 1
  • 12
1
 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);
});

}

if it is with "ontent://gmail-ls/myemail@gmail.com/messages/520/attachments/0.1/BEST/false" case 1 .this works