User42590
User42590

Reputation: 2531

Attach image from resource with Email Body Android

I have an app in which there is an email section where I have to entered text and image in body. I searched on net but did not find corresponding solution. Any help will would be appreciated.

Here is the View :

Google Email/Default Email Client

Upvotes: 0

Views: 1228

Answers (1)

Abhishek Agarwal
Abhishek Agarwal

Reputation: 1897

BitmapFactory.Options bitmapFatoryOptions=new BitmapFactory.Options();
        bitmapFatoryOptions.inPreferredConfig=Bitmap.Config.ARGB_8888;
Bitmap myBitmap=BitmapFactory.decodeResource(getResources(),R.drawable.face4,bitmapFatoryOptions);

 File  mFile = savebitmap(myBitmap);
        Uri u = null;
        u = Uri.fromFile(mFile);
        Intent emailIntent = new Intent(Intent.ACTION_SEND);
        emailIntent.setType("image/*");
   Intent.setClassName("com.google.android.gm", "com.google.android.gm.ComposeActivityGmail");
        emailIntent.putExtra(Intent.EXTRA_SUBJECT, "Send Mail");
        emailIntent.putExtra(Intent.EXTRA_TEXT,  getString(R.string.share_texthere));
        emailIntent.putExtra(Intent.EXTRA_STREAM, u);
        startActivity(Intent.createChooser(emailIntent,"Send"));

Code for saveBitmap() Method:

    private File savebitmap(Bitmap bmp) {
    String temp="SplashItShare";
    String extStorageDirectory = Environment.getExternalStorageDirectory().toString();
    OutputStream outStream = null;
    String path = Environment.getExternalStorageDirectory()
            .toString();
    new File(path + "/SplashItTemp").mkdirs();
    File file = new File(path+"/SplashItTemp", temp + ".png");
    if (file.exists()) {
        file.delete();
        file = new File(path+"/SplashItTemp", temp + ".png");
    }

    try {
        outStream = new FileOutputStream(file);
        bmp.compress(Bitmap.CompressFormat.PNG, 100, outStream);
        outStream.flush();
        outStream.close();
    } catch (Exception e) {
        e.printStackTrace();
        return null;
    }
    return file;
}

Hope It will work for you.

Upvotes: 1

Related Questions