Question

I'm working on Titanium SDK 2.1.3 and deploying for Android, I followed this article to include my app in the list of applications that can open mime types like application/*, image/* and audio/*. It made my Android Manifest to look like this:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="nenvo.com.desktop" android:versionCode="1"
    android:versionName="1">
    <uses-sdk android:minSdkVersion="8" />

    <!-- TI_MANIFEST -->

    <application android:icon="@drawable/appicon"
        android:label="Desktop Lite" android:name="DesktopLiteApplication"
        android:debuggable="false">

        <!-- TI_APPLICATION -->

        <activity android:name=".DesktopLiteActivity"
            android:label="Desktop Lite" android:theme="@style/Theme.Titanium"
            android:configChanges="keyboardHidden|orientation">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
            <intent-filter>
                <data android:scheme="content" />
                <action android:name="android.intent.action.VIEW" />
                <category android:name="android.intent.category.DEFAULT" />
                <category android:name="android.intent.category.BROWSABLE" />
                <!-- open any mime type file -->
                <data android:mimeType="application/*" />
                <data android:mimeType="audio/*" />
                <data android:mimeType="image/*" />
                <data android:mimeType="text/*" />
                <data android:mimeType="video/*" />
            </intent-filter>
        </activity>



        <activity android:name="org.appcelerator.titanium.TiActivity"
            android:configChanges="keyboardHidden|orientation" />
        <activity android:name="org.appcelerator.titanium.TiTranslucentActivity"
            android:configChanges="keyboardHidden|orientation"
            android:theme="@android:style/Theme.Translucent" />
        <activity android:name="org.appcelerator.titanium.TiModalActivity"
            android:configChanges="keyboardHidden|orientation"
            android:theme="@android:style/Theme.Translucent" />
        <activity android:name="ti.modules.titanium.ui.TiTabActivity"
            android:configChanges="keyboardHidden|orientation" />
        <activity android:name="ti.modules.titanium.ui.android.TiPreferencesActivity" />

        <service android:name="org.appcelerator.titanium.analytics.TiAnalyticsService"
            android:exported="false" />


    </application>

    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
    <uses-permission android:name="android.permission.ACCESS_MOCK_LOCATION"/>
    <uses-permission android:name="android.permission.VIBRATE"/>
    <uses-permission android:name="android.permission.ACCESS_WIFI_STATE"/>
    <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>
    <uses-permission android:name="android.permission.INTERNET"/>
    <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>


</manifest>

Now my app is launched when an email attachment is trying to be opened, the problem is that I don't know how to retrieve the file I'm trying to open, I tried the following on my start and resume events for my Android application:

activity.addEventListener('start', function() {
    Ti.API.log('*** Start Event Called ***');
    var args = activity.getIntent().getData(); // this is null
    functions.handleFunction(args); 
});

activity.addEventListener('resume', function() {
    Ti.API.log('*** Resume Event Called ***');
    var args = activity.getIntent().getData(); //this is also null
    functions.handleFunction(args);
});

When I tried to retrieve the data for the intent, I got null as a result. When I display the Intent object, I get the following:

{"flags":0,"type":null,"data":null,"action":null}

Why am I not receiving any data on the Intent? Am I doing something wrong?

EDIT

I noticed that when I opened my app from the list of apps that could open an specific file, my app would start as if it was closed before (even if it had been opened previously). I tried to get the Intent during this loading on my app.js file and to my surprise I got the data of the intent, which is the following:

content://com.android.email.attachmentprovider/1/2/RAW

How do I open that? How do I retrieve the file from that address so I can save it into my application directory?

EDIT 2

I tried to get the file from that address like this:

var tmpFile = Ti.Filesystem.getFile(String(currentActivity.getIntent().getData()));

But what I got was an File object that looked like this:

{
    "writable": false,
    "symbolicLink": false,
    "parent": null,
    "readonly": true,
    "nativePath": "content://com.android.email.attachmentprovider/1/2/RAW",
    "executable": false,
    "name": "api-eventos.txt",
    "directoryListing": null,
    "hidden": false,
    "size": 0
}

It does show the name of the original file, but the size is 0 and there doesn't seem like it contains any information of the original file, I tried to read it and got null as a result. How can I get the file from the attachment address?

Was it helpful?

Solution

Finally found the way to solve this. The content returns an URI, I can use it to get the file but I can't read it, but I can copy it into another file. The new file can be then read and being used like any other File object. Here's my code:

var currentActivity = Ti.Android.currentActivity;
var passedData = currentActivity.getIntent().getData();
if(passedData !== null)
{
    var tmpFile = Ti.Filesystem.getFile(String(passedData));
    var fileData = Ti.Filesystem.getFile(Ti.Filesystem.getTempDirectory() + tmpFile.name);
    tmpFile.copy(fileData.nativePath); //the copy can be handled like any other normal file
    Ti.App.Properties.setString('passedData', JSON.stringify(fileData)); // in my case I place it in a Ti.App.Property for later use
}

I hope this helps others in the same situation I was. This works for both 2.1.3 and 3.0.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top