0

I am trying to send image as attachment using Send mail. but I am unable to get the image from resource , but if I try to send the image saved in sd card then it get successful send. here is the code which I use to get image from resource.

 DataSource fds = new FileDataSource(
                         "android.resource://com.example.birth/" + R.raw.ic_mail2);

2 Answers2

0

Since you say that adding a file from the SDCard works, why don't you reate a temporary file outside of your project folder from the Drawable, for instance in the SDCard or the internal storage. Then use it in the email or wherever you want to attach the file. Finally delete the temporary file.

Related to complete the suggested solution

How can I write a Drawable resource to a File?

Community
  • 1
  • 1
Juan Cortés
  • 20,634
  • 8
  • 68
  • 91
0

It should be an answer and not a comment so I'm posting the solution that previously I commented. So to get the image from resource you should use this:

Drawable drawable = ContextCompat.getDrwable(context, R.drawable.yourImage);

getResources().getDrawable(); is deprecated in api 22. You should use the ContextCompat solution.

Asama
  • 385
  • 2
  • 14