Is it possible to attach an Android res / drawable file to an email using JavaMail?

1

I'm looking for the solution in two ways:

  • Get the file PATH:

I would like to get the path of an image that is in res/drawable in my Android project.

I'm needing path because I have to send it attached by email, and I'm using JavaMail for this and it only accepts a path or a File , for attached file.

  • Make JavaMail be able to accept a Bitmap:

Change how to create the email attachment, today I'm doing this:

MimeMultipart content = new MimeMultipart();
MimeBodyPart attachmentPart = new MimeBodyPart();
// aqui teria que setar um bitmap ao inves de um Path ou File;
attachmentPart.attachFile(attachment);
attachmentPart.setContentID("<image>");
attachmentPart.setDisposition(MimeBodyPart.INLINE);
content.addBodyPart(attachmentPart);

I'll try to explain the problem a little better:

I need this if I do not find a user-configured logo (in a specific folder with a specific name in the sdcard) for email signature, then I would use a standard logo of my application, and this standard logo is available in my resources (res / drawable), since it is immutable.

  

I saw the solution that @ramaral posted, but in spite of being good, it is not a solution that I expected, I believe there must be another way, as having to make a copy of the image to the sdcard. I do not think it smells good.

Final Solution with the help of the cited answers

Using ByteArrayDataSource as quoted by @Elcio Arthur Cardoso in his response , I changed my method that includes the attachment to allow attaching a file to byte[] like this:

MimeMultipart content = new MimeMultipart();

// ... add message part

// converter bitmap to byte[]
Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.transparent_1x1);
ByteArrayOutputStream stream = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.PNG, 100, stream);
byte[] imageBytes = stream.toByteArray();

// criar anexo do email, apartir de um array de byte (byte[])
MimeBodyPart attachmentPart = new MimeBodyPart();
DataHandler handlerAttach = new DataHandler(new ByteArrayDataSource(imageBytes));
attachmentPart.setDataHandler(handlerAttach);
attachmentPart.setFileName("image.png");
attachmentPart.setContentID("<image>");
attachmentPart.setDisposition(MimeBodyPart.INLINE);
content.addBodyPart(attachmentPart);

This solution worked perfectly for me.

    
asked by anonymous 21.03.2014 / 15:49

3 answers

1

You should read the file in the way @ ramaral spoke :

Resources resources= getResources();
Bitmap bitMap = BitmapFactory.decodeResource(resources, R.drawable.image);

but instead of writing to the SDCard, you can attach directly using #

These links should help:

29.05.2014 / 18:02
3

You can not directly access the path or file because resources is compiled with the application.

What you can do is create a Bitmap from resource .

Resources resources= getResources();
Bitmap bitMap = BitmapFactory.decodeResource(resources, R.drawable.image);

Bitmap can be written to SdCard , then you can send it by email. See here as burn.

    
21.03.2014 / 17:02
3

Searching, I found a solution that I could not test because I'm not at home ...

At first, you can access your app's drawable with a URI with android.resource protocol.

To access a drawable through File would be:

File image = new File("android.resource://package_do_app/"+ R.drawable.image);

As I said earlier, I could not test, if the code does not work I delete the answer. If I am violating some OS rule, I apologize in advance.

The source is this Official SO . Another source with more details .

As you can see, the above form is not uniform for all versions of Android.

So, by analyzing the documentation for MimeBodyPart of JavaMail, there is a constructor that receives a InputStream , and as in Android it is possible to get a InputStream from a Drawable that is strictly located in the < b> raw .

To recover a InputStream from a Drawable that is in the resource/raw folder I do this:

public class AssetsHelper {
    public static InputStream readRawResource(Context context, int rawResId) {
        return context.getResources().openRawResource(rawResId);
    }
}

From your code, I would do something like:

MimeBodyPart attachmentPart = new MimeBodyPart(AssetsHelper.readRawResource(getApplicationContext(), R.raw.transparent1x1);
//... Restante do código

Placing your image in res/raw should work.

    
23.05.2014 / 16:37