I've been pushing ahead on this project.  I'd like to report my
progress for the benefit of anyone who comes across this thread.  And
perhaps someone can tell me if there's a better solution than what I'm
doing.

I'm trying to export data from my program in a way that users can
easily share: emailing it, posting it on a website, etc.  And when
someone opens that file in their mail client, web browser, etc., I
want my program to automatically open to import it.  This requires me
to identify the file as belonging to my program.  With Android, there
are two ways you can try to specify file types: by MIME type and by
filename extension.  Each of these only works in a minority of cases.
By using both of them, you can cover a slightly larger set of cases,
though still far from all cases.  More on that later.

The export side is quite easy.  First, I create a temporary file with
the proper filename extension:

File tempFile = File.createTempFile("Export", ".mytype");

I write the data to that temporary file, then export it as follows:

startActivity(new Intent(Intent.ACTION_SEND).setType("application/
vnd.mytype").putExtra(Intent.EXTRA_STREAM,
android.net.Uri.fromFile(tempFile)));

This works beautifully.  It immediately opens the Gmail app to send an
email and attaches my file to it.  Presumably if I used a different
email client that would be opened, though I haven't tested this.

Now for the import side.  I use the following intent filter to
recognize files by MIME type:

<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:mimeType="application/vnd.mytype"/>
</intent-filter>

When I open the email and press the Preview button next to the
attachment, it opens my app just as I wanted.

This only works as long as you have the MIME type associated with the
file.  If you post it on a website, that association gets lost.  In
that case we need to use filename extension:

<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="http" />
  <data android:scheme="https" />
  <data android:scheme="ftp" />
  <data android:scheme="file" />
  <data android:scheme="content" />
  <data android:host="*" />
  <data android:pathPattern=".*\\.mytype" />
</intent-filter>

Now when I press the link in the Browser app, it brings up a list of
applications to open the file with, mine being one of the options.
That's not quite ideal, but it's good enough.

Unfortunately, there are lots of other cases that don't work.  Here's
a really basic one:

Open the email on your computer and save the attached file to disk.
Now attach it to a new email.  This causes the MIME type to be lost.
When you open that email in the Gmail app, the Preview button is no
longer there.  It seems that when an attachment doesn't have a MIME
type specified, the Gmail app doesn't let you do *anything* with.  Not
view it, not even save it to the sdcard, which is just ridiculous.
That really needs to be fixed.

Of course, what Android really needs is a way to define custom
filename extension to MIME type mappings.  Then this whole issue would
go away.  But as far as I can tell, there's no way to do that.

Here's another case that doesn't work: view the email in the Browser
using the Gmail web interface.  The URL it provides to download the
attachment doesn't include the attachment name, nor is the MIME type
specified.  Hence, you can't open it in my app.

As a last resort, I plan to include an Import command in my app that
instructs the user to save the file to their sdcard in /downloads,
then load it from there.  I'd feel better about that as a real
solution if the Gmail app would actually let you save files to the
sdcard.

Peter

-- 
You received this message because you are subscribed to the Google
Groups "Android Developers" group.
To post to this group, send email to android-developers@googlegroups.com
To unsubscribe from this group, send email to
android-developers+unsubscr...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en

Reply via email to