Description

Problem

Using the current array of functions available to Ti.Android.Intent, there is no way to get binary data from a Ti.Android.EXTRA_STREAM extra. Ti.Android.EXTRA_STREAM wants to return an Android URI, which we have no means to return. In addition, URIs will be tough to resolve on the developer's end unless they are just passing them to another Intent.

For example, a very common case is that a developer would like to use an Android Intent Filter in their Titanium app to be able to receive SEND Intents for things like images. Often a user will want to show that image in their Titanium app immediately. A URI isn't going to do them much good, unresolved. To that end a Ti.Android.Intent.getBlobExtra() function would be extremely useful.

Proposed Usage

var win = Ti.UI.createWindow({

backgroundColor: '#fff',

fullscreen: false,

exitOnClose: true

});

win.addEventListener('open', function(e) {

var intent = Ti.Android.currentActivity.getIntent();

var iname = Ti.Android.EXTRA_STREAM;

if (intent && intent.hasExtra(iname)) {

// Create ImageView from TiBlob

var blob = intent.getBlobExtra(iname);

win.add(Ti.UI.createImageView({

image: blob,

height: 300,

width: 300,

left: 0,

top: 0

}));

} else {

Ti.API.info('No extra named "' + iname + '" found in Intent');

}

});

win.open();

As this code reacts to an Intent, it will require that an Intent Filter is setup to receive it. Use the filter below in a custom AndroidManifest.xml. Make sure this Intent Filter is inside the main activity.

<intent-filter>

<action android:name="android.intent.action.SEND" />

<category android:name="android.intent.category.DEFAULT" />

<data android:mimeType="image/*" />

</intent-filter>

To test, long press on an image in the android gallery and select "share". This test code should then appear as an option in the Intent Filter list.

Additional Notes

TIMOB-3448 is a very similar issue. That issue requests access to the underlying URI to be able to pass it to another Intent. What I am proposing would be to take it a step further by resolving the URI behind the scenes and returning the data as a blob.

TIMOB-7249 should be addressed before, or shortly after, this feature request is included in titanium_mobile. TIMOB-7249 shows that the getData(), getAction(), and getType() functions do not return the appropriate values. This will be a problem for any app that uses an Intent Filter that attempts to handle more than one type of data.

Bill Dawson
added a comment - 13/Jun/12 7:14 AM - edited Additional Testing Notes
Here's how to get this to work (the part about the additions to the Android Manifest might be a little confusing).
Create an app with the code Tony put in above.
After you create the app, create a platform/android folder.
Copy build/android/AndroidManifest.xml to platform/android/AndroidManifest.xml. This makes a custom AndroidManifest.xml.
Open platform/android/AndroidManifest.xml.
Find the section for the default activity. It will look like this (though the name might be different in your case):
<activity android:name=".Timob7254Activity"
android:label="timob7254" 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>
</activity>
See that "intent-filter" section? You'll want to make a second one right below it, using the intent-filter Tony provided above. So in my example it will end up looking like:
<activity android:name=".Timob7254Activity"
android:label="timob7254" 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>
<action android:name="android.intent.action.SEND" />
<category android:name="android.intent.category.DEFAULT" />
<data android:mimeType="image/*" />
</intent-filter>
</activity>
Save that file.
Build and install the app on a device/emulator that has some photos in its photo gallery.
Go to the device's photo gallery and long-press a photo, then select "Share" (or "Send" or whatever).
You'll see a list of installed applications that are able to share/send a photo. The app you just created should be listed among them – choose it.
The app should open and display the photo.

I'm having a bit of a trouble with this function, I did exactly as followed but my App throws an exception saying Uncaught TypeError: Object #<intent> has no method 'getBlobExtra'

I suppose I have to reference a library in Titanium, but I'm having a difficult time understanding where and what to to reference. I even have SDK 2.1.0 installed. I simply just wanted to fetch my image path from the gallery so that I can upload it but that did not work traditionally and through hours of reading I ended up here.

Smbat Yeranyan
added a comment - 29/Jun/12 8:38 PM I'm having a bit of a trouble with this function, I did exactly as followed but my App throws an exception saying Uncaught TypeError: Object #<intent> has no method 'getBlobExtra'
I suppose I have to reference a library in Titanium, but I'm having a difficult time understanding where and what to to reference. I even have SDK 2.1.0 installed. I simply just wanted to fetch my image path from the gallery so that I can upload it but that did not work traditionally and through hours of reading I ended up here.
Thanks in advance

Priya Agarwal
added a comment - 02/Jun/14 11:02 PM Closing the issue since it is not reproducible.
Followed steps by Bill Dawson & the app does not crash. Selecting the app to share the pic from the gallery shows the shared pic in the app.
But on importing and using the app from titanium_mobile_tests_internal/FeatureTest/Android/ TIMOB-7254 app crashes.Hence test app needs modification.
Verified with Environment:
Appc-Studio:3.3.0.201405271647
sdk:3.3.0.v20140602174112
acs:1.0.14
alloy:1.4.0-beta
npm:1.3.2
titanium:3.3.0-beta2
titanium-code-processor:1.1.1
xcode:5.1.1
Device:Nexus7(v4.4.2)
OS:Maverick(10.9.2)
Hence closing the issue.