Khurram W. Malik
Khurram W. Malik

Reputation: 2895

Attaching an image to an email via android app

My app successfully makes email but when I try to attach an image the code crashes here is the code snippet

        Intent i = new Intent(Intent.ACTION_SEND);
        i.setType("message/rfc822");
        i.putExtra(Intent.EXTRA_EMAIL  , new String[]{"email address"});
        i.putExtra(Intent.EXTRA_SUBJECT, "subject of email");
        i.putExtra(Intent.EXTRA_TEXT   , "body of email");
    //  i.putExtra(Intent.EXTRA_STREAM, R.drawable.pic4);



        try {
            startActivity(Intent.createChooser(i, "Send mail..."));
        } catch (android.content.ActivityNotFoundException ex) {
            Toast.makeText(EidCards.this, "There are no email clients installed.", Toast.LENGTH_SHORT).show();
        }

the line which is commented makes the code crash please suggest a way out !

Upvotes: 0

Views: 1329

Answers (2)

Khurram W. Malik
Khurram W. Malik

Reputation: 2895

well it was simple :P

Intent i = new Intent(Intent.ACTION_SEND);

        int imageURI=R.drawable.pic4;

        i.setType("message/rfc822");
        i.putExtra(Intent.EXTRA_EMAIL  , new String[]{"email address"});
        i.putExtra(Intent.EXTRA_SUBJECT, "subject of email");
        i.putExtra(Intent.EXTRA_TEXT   , "body of email");
        i.putExtra(Intent.EXTRA_STREAM, R.drawable.pic4);

            i.putExtra(Intent.EXTRA_STREAM, Uri.parse("android.resource://"+getPackageName()+"/"+imageURI));


        try {
            startActivity(Intent.createChooser(i, "Send mail..."));
        } catch (android.content.ActivityNotFoundException ex) {
            Toast.makeText(EidCards.this, "There are no email clients installed.", Toast.LENGTH_SHORT).show();
        }

Upvotes: 1

wojciii
wojciii

Reputation: 4325

This does not work for you:

i.putExtra(Intent.EXTRA_STREAM, R.drawable.pic4);

What is the type of R.drawable.pic4? Is that not an integer used to resolve the drawable?

See how others are attaching images: Sharing an image with Google+ app using Intent.ACTION_SEND and Intent.EXTRA_STREAM

Upvotes: 1

Related Questions