4

I have a simple app which I am using to spike sending attachments.

I have managed to send a text file from the SD card (although I couldn't get this to work with a file created in the apps private area using openFileOutput(fileName, 0)) but I now want to send a database.

By debugging I can verify the database exists and has an entry in its only table. My code to send looks like this:

gmailButton = (Button) findViewById(R.id.button);
gmailButton.setOnClickListener(new OnClickListener()
{
    @Override
    public void onClick(View v)
    {
        Intent sendIntent = new Intent(Intent.ACTION_SEND); 
        sendIntent.putExtra(Intent.EXTRA_SUBJECT, "subject line"); 
        sendIntent.putExtra(Intent.EXTRA_TEXT,"Body of email"); 
        Uri uri = Uri.fromFile(getDatabasePath("TEST_DB"));
        //uri = file:///data/data/com.gmailspike/databases/TEST_DB
        sendIntent.putExtra(Intent.EXTRA_STREAM, uri); 
        sendIntent.setType("application/octet-stream");

        startActivity(Intent.createChooser(sendIntent,"Email:"));
    }
})

;

However, when the email client opens the attachment has a size of 0 bytes and if I touch to open the attachment the client says the file cannot be found.

Any ideas? I'm not sure the mime type is correct, or even if it is important!

Lukas Knuth
  • 25,449
  • 15
  • 83
  • 111
barry
  • 4,037
  • 6
  • 41
  • 68
  • Have a look at the following SO Answer for a really great example on copying the DB file, sending the email and deleting the copied DB file: http://stackoverflow.com/questions/18548255/android-send-xml-file-doesnt-send-attachment/18548685#18548685 – Joshua Pinter Dec 05 '13 at 06:06

2 Answers2

4

I remember having had an issue like this too, when trying to send an email with an image attachment, linking to a file stored in the apps private data folder. The attachment is just missing.

Using the mail intent opens a new application to handle the mail-creation. Therefore you either need to write a Content Provider to allow other applications access to your private data.

Or copy the content to public area first and add it to the mail intent from there (this only works when a SD-Card is present with most phones). The External Storage.getExternalStorageDirectory() could maybe used in that case.

Hope this helps to find a solution.

sunadorer
  • 3,855
  • 34
  • 42
  • Ahhh... I haven't delved into content providers yet so will copy to external storage for starters. – barry Aug 23 '11 at 22:09
1

As for the MIME type, it is surely important, since an application has to support it (explicitly or through wildcard) to respond to the intent and to be found via createChooser.

I think Gmail for instance accepts */* as MIME type with ACTION_SEND but I don't know about other mail clients.

Edit : And as for the permission, have a look at the FLAG_GRANT_READ_URI_PERMISSION flag from the Intent class : http://developer.android.com/reference/android/content/Intent.html#FLAG_GRANT_READ_URI_PERMISSION

darma
  • 4,687
  • 1
  • 24
  • 25
  • Thanks for the interesting info re mime types. I tried the permission flag but it didn't work. I think I'll copy the file to external storage first. – barry Aug 23 '11 at 22:07