Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Intent with setType("message/rfc822") for Android API Level before 2.3.3

I have a problem with setting type "message/rfc822" for intent to send e-mail with file attachment on Android emulator. I have to use setType("message/rfc822") because the file doesn't have standard MIME-type (SQLite database) and I am trying to avoid a lot of applications in the select list for user's choice. For all API Levels before 2.3.3 I have an error:

java.lang.RuntimeException: 
Unable to start activity ComponentInfo{my.cashwatcher/my.cashwatcher.SendEmailActivity}: 
android.content.ActivityNotFoundException: 
No Activity found to handle Intent { act=android.intent.action.SEND typ=message/rfc822 
(has extras) }

In the case of API Level, 2.3.3 code works fine and error doesn't appear. Is it a problem with the Android emulator or old APIs!?

Code:

Intent sendIntent = new Intent(Intent.ACTION_SEND);                         
sendIntent.setType("message/rfc822");            
sendIntent.putExtra(Intent.EXTRA_EMAIL , new String[]{appPrefs.getEmail("email")});                   
sendIntent.putExtra(Intent.EXTRA_STREAM, Uri.fromFile(new File(Environment.getExternalStorageDirectory(), DATABASE_PATH)));
sendIntent.putExtra(Intent.EXTRA_TEXT, "body_of_email"); 
sendIntent.putExtra(Intent.EXTRA_SUBJECT, "APPLICATION_NAME");
startActivityForResult(sendIntent, EMAIL_SEND_RESULT);
like image 931
isabsent Avatar asked Dec 17 '22 07:12

isabsent


2 Answers

First, "to avoid a lot of applications in select list for user's choice", use ACTION_SENDTO and a mailto: Uri.

Second, what you are experiencing is not "a problem of Android emulator" nor "old APIs". You need 1+ applications that are capable of handling the ACTION_SEND Intent and a MIME type of message/rfc822. There is no guarantee that any given device will support that combination, let alone any given emulator. Your code needs to handle that, just as if you use ACTION_GOBBLEDYGOOK or a MIME type of thisis/sonotreal or whatever.

like image 63
CommonsWare Avatar answered Dec 28 '22 06:12

CommonsWare


I have made an application that uses URI example as you desired:

if(v.getId()==R.id.button3)
{
    intent=new Intent(Intent.ACTION_SEND);
    intent.setData(Uri.parse("mailto"));
    String[]to={"[email protected]","[email protected]"};
    intent.putExtra(Intent.EXTRA_EMAIL, to);
    intent.putExtra(Intent.EXTRA_SUBJECT, "hello");
    intent.putExtra(Intent.EXTRA_TEXT, "hi");
    intent.setType("message/rfc822");
    chooser=intent.createChooser(intent, "send mail");
    startActivity(chooser); 
}
like image 41
akshay katheria Avatar answered Dec 28 '22 07:12

akshay katheria