Titanium Mobile
  1. Titanium Mobile
  2. TIMOB-7254

Android: Intent - return EXTRA_STREAM data as blob

    Details

    • Type: New Feature New Feature
    • Status: Closed
    • Priority: Medium Medium
    • Resolution: Fixed
    • Affects Version/s: Release 1.8.0.1
    • Fix Version/s: Release 2.1.0, Sprint 2012-08
    • Component/s: Android
    • Environment:

      Titanium SDK version: 1.8.0.1
      Javascript Engine: Rhino & V8
      Platform & version: Android 2.2+
      Host Platform: Mac OSX 10.7.2
      Titanium Studio version: 1.0.7.201112132151

      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.

        Activity

        Hide
        Tony Lukasavage added a comment -

        I'm working on a pull request for this feature now. Just needed to create a corresponding Jira ticket first.

        Show
        Tony Lukasavage added a comment - I'm working on a pull request for this feature now. Just needed to create a corresponding Jira ticket first.
        Hide
        Bill Dawson added a comment - - 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.
        Show
        Bill Dawson added a comment - - 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.
        Hide
        Natalie Huynh added a comment -

        Tested with Nexus One 2.2.2 with 2.1.0.v20120612170248

        Show
        Natalie Huynh added a comment - Tested with Nexus One 2.2.2 with 2.1.0.v20120612170248
        Hide
        Smbat Yeranyan added a comment -

        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

        Show
        Smbat Yeranyan added a comment - 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

          People

          • Assignee:
            Bill Dawson
            Reporter:
            Tony Lukasavage
          • Watchers:
            1 Start watching this issue

            Dates

            • Created:
              Updated:
              Resolved:

              Development