开发者

Sending Sqlite database as email attachment in Android

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 f开发者_如何学JAVAile 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!


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.


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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜