10

I have a requirement to attach a Zip file to a message in the Android email composer.

The Zip file is being created by my application and stored in the app's private storage area (accessible via getFilesDirectory()). I am obtaining the URI to this Zip file and appending it to an email intent object; when I launch the email activity I am able see the attachment but my email recipient is not receiving the files,

After doing some research on this, I found that my application data cannot be shared with other app's (in this case the Android Email app).

To solve this problem I would like to implement the recommend solution of using a content provider to share my application data.

First of all, I would like to confirm if this is possible, and if it is could anyone please give me some hints on how to go about doing this. I need to know how to copy my Zip file from the getFilesDirectory() of my application, to the content provider, and also how to attach the content provider URI to the email intent object.

I can't place my zip files into an SD card...

I just want only to store my files in to my device internal storage and attach to the email composer.

brig
  • 305
  • 2
  • 6
  • 12
  • Just to understand, you want to be able to store files on your phone and then email them? – nickfox Jun 30 '11 at 12:28
  • Storing the zip file into your apps personal data directory makes it only viewable to your app. The intent firing off to email app wont be able to read into your app's protected data. You must save to SD as an intermediary. – yep Jun 30 '11 at 12:32
  • @nickfox , yes I want to be able to store files on your phone and then email them. but I don't want to store my files in SD card. – brig Jun 30 '11 at 12:40
  • @yep , can't we do it using ContentProvider mechanism..??? – brig Jun 30 '11 at 12:42
  • @yep Your comment about the visibility of files created by openFileOutput() is inaccurate, see my answet to the original posting. – class stacker Jan 07 '13 at 13:03

2 Answers2

5

By default, a ContentProvider can be accessed by any application on the device. If you are willing for these files to be accessed by any application on the device, create a ContentProvider, with real implementations for getType() and openFile(). Then, the content:// URL should work with the Email app, AFAIK.

Here is a sample project demonstrating a ContentProvider serving up files from local storage, in this case to a WebView.

CommonsWare
  • 986,068
  • 189
  • 2,389
  • 2,491
0

If all you want is to avoid storage on the external SD card, then you don't need a ContentProvider. You can live with

openFileOutput("yourzipfile.zip", MODE_WORLD_READABLE)

and pass

putExtra(Intent.EXTRA_STREAM, Uri.fromFile(getFileStreamPath("yourzipfile.zip))

along with your ACTION_SEND Intent.

However, the email program will probably set no Content-type.

Also, there's no reliable way to tell when the email app doesn't need your file anymore. This means that you either risk ending up with many files or sending a newer content as the attachment of an older email.

Another issue with this solution is that everyone can read your zip file. This problem would not occur with a ContentProvider solution, where you can grant access permission on a per-Intent basis, i.e. allowing access to one file only for one email Intent.

Matching of the Uri is then done via the URI, which may start with your package name, such as

content://com.yourdomain.yourapp.yourproviderclass/some/path

You may want to look at http://developer.android.com/guide/topics/manifest/provider-element.html

class stacker
  • 5,357
  • 2
  • 32
  • 65