1
I’m looking for a two-way solution:
- Get the file path:
I would like to get the path of an image that is in res/drawable on my Android project.
I’m in need of the path because I have to send it in attachment by email, and I’m using Javamail for this and he only accepts one path or a File, for attached file.
- Getting Javamail to accept a Bitmap:
Change way of creating the email attachment, today I’m doing so:
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 so that if I don’t find a user-configured logo (in a specific folder and with a specific name on sdcard) for email signature, then I would use a default logo from my application, and this default logo is available in my Sources (res/drawable)since he is unchanging.
I saw the solution that @ramaral posted, but although good, it’s not a solution that I expected, I believe there must be another way, because I have to make a copy of the image for sdcard. I don’t think "smell good".
Final solution with the help of the answers cited
Using the Bytearraydatasource as quoted by @Elcio Arthur Cardoso in his reply, I switched my method that includes the attachment to allow attaching a file on byte[] thus:
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);
That solution worked perfectly for my case.
even so from a
BitmapI can’t even get thepathnor theFile, to be able to set my Attachment. Or there is some way?– Fernando Leal
The
Bitmapcan be recorded in theSdCard. Behold here how to do– ramaral
Thanks for the help, I managed to solve, from your tips and colleagues, check my solution and if you have tips for improvements, feel free to edit it.
– Fernando Leal