How to Attach Image from Drawable to Gmail

How to attach image from drawable to gmail?

Here is the working code which you need:

Firstly save image from Drawable to SD Card here is the code:

try{

Bitmap largeIcon = BitmapFactory.decodeResource(getResources(), R.drawable.bubble_green);

//replace "R.drawable.bubble_green" with the image resource you want to share from drawable

ByteArrayOutputStream bytes = new ByteArrayOutputStream();
largeIcon.compress(Bitmap.CompressFormat.JPEG, 40, bytes);

// you can create a new file name "test.jpg" in sdcard folder.
File f = new File(Environment.getExternalStorageDirectory() + File.separator + "test.jpg");

f.createNewFile();

// write the bytes in file
FileOutputStream fo = new FileOutputStream(f);
fo.write(bytes.toByteArray());

// remember close de FileOutput
fo.close();
}
catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

Then get the saved image from SD card and attach in the email intent like this:

        Intent shareIntent = new Intent(android.content.Intent.ACTION_SEND);
shareIntent.setType("image/jpeg");

shareIntent.putExtra(android.content.Intent.EXTRA_SUBJECT, "Hi"); //set your subject
shareIntent.putExtra(android.content.Intent.EXTRA_TEXT, "How are you"); //set your message

String imagePath = Environment.getExternalStorageDirectory() + File.separator + "test.jpg";

File imageFileToShare = new File(imagePath);

Uri uri = Uri.fromFile(imageFileToShare);

shareIntent.putExtra(Intent.EXTRA_STREAM, uri);

startActivity(Intent.createChooser(shareIntent, "Share Image"));

how to attach image from drawable folder to gmail in android?

Intent intent = new Intent(Intent.ACTION_SEND);
intent.setType("text/plain");

Uri path = Uri.parse("android.resource://your.package.name/" + R.drawable.sample_1);
intent.putExtra(Intent.EXTRA_STREAM, uri);

share image from drawable in android

replace this line in your coding

intent.setType("image/*");

instead of

intent.setType("image/jpeg");

How to send mail with an image as an attachment in android?

 public class MailImageFile extends javax.mail.Authenticator {

public MailImageFile(){}

public void Mail(String user, String pass) {
Properties props = new Properties();
props.put("mail.smtp.host", "smtp.gmail.com");
props.put("mail.smtp.socketFactory.port", "465");
props.put("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory");
props.put("mail.smtp.auth", "true");
props.put("mail.smtp.port", "465");

Session session = Session.getDefaultInstance(props, new javax.mail.Authenticator() {
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication("abc@gmail.com", "pqr123%");
}
});
try {
Message message = new MimeMessage(session);
message.setFrom(new InternetAddress("abc@gmail.com"));
message.setRecipients(Message.RecipientType.TO, InternetAddress.parse("xyz@gmail.com"));
message.setContent(_multipart);
message.setSubject("Testing Subject");
message.setContent("Hi...", "text/html; charset=utf-8");

Transport.send(message);

} catch (MessagingException e) {
throw new RuntimeException(e);
}

//Got this solution form here

    private Multipart _multipart; 
_multipart = new MimeMultipart();

public void addAttachment(String filename,String subject) throws Exception {
BodyPart messageBodyPart = new MimeBodyPart();
DataSource source = new FileDataSource(filename);
messageBodyPart.setDataHandler(new DataHandler(source));
messageBodyPart.setFileName(filename);
_multipart.addBodyPart(messageBodyPart);

BodyPart messageBodyPart2 = new MimeBodyPart();
messageBodyPart2.setText(subject);

_multipart.addBodyPart(messageBodyPart2);
}

}

How to make initials icon like in Gmail?

You need to create a drawable resource file under res>drawable and save it as bg_circle.xml:

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="oval">
<solid android:color="#424242"/>
<size android:width="120dp" android:height="120dp"/>
</shape>

Now make icon_container in your layout.xml file as:

<RelativeLayout
android:id="@+id/icon_container"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<ImageView
android:layout_width="@dimen/icon_width_height"
android:layout_height="@dimen/icon_width_height"
android:src="@drawable/bg_circle" />
<TextView
android:id="@+id/icon_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:textColor="@android:color/white"
android:textSize="@dimen/icon_text" />
</RelativeLayout>

For complete code implementation visit Implementing Gmail like icon with text and random colors

How to show an image in the email body?

Unfortunately, it's not possible to do this with Intents.

The reason why for example bold text is displayed in the EditText and not an Image is that StyleSplan is implementing Parcelable whereas ImageSpan does not. So when the Intent.EXTRA_TEXT is retrieved in the new Activity the ImageSpan will fail to unparcel and therefor not be part of the style appended to the EditText.

Using other methods where you don't pass the data with the Intent is unfortunately not possible here as you're not in control of the receiving Activity.



Related Topics



Leave a reply



Submit