0

This is my code

   String to = Tothem.getText().toString();
    String message = msg.getText().toString();
    Intent email = new Intent(Intent.ACTION_SEND);
    email.setType("image/*");

    email.putExtra(Intent.EXTRA_EMAIL, new String[]{to});
    email.putExtra(Intent.EXTRA_TEXT, message);
    email.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
    Uri uri = Uri.parse("android.resource://" + getPackageName() + "/" + R.drawable.bd2);
    email.putExtra(Intent.EXTRA_STREAM, uri);


    email.setType("message/rfc822");

    startActivity(Intent.createChooser(email, "Choose an Email client :"));
}

but when I receive the email is with out a format, I have to select a program to open it Heres how I receive the email

2 Answers2

0

use my code

      File image = new File(Uri.parse("android.resource://" + C.PROJECT_PATH + "/drawable/" + R.drawable.icon_to_share).toString());
      email.putExtra(Intent.EXTRA_STREAM, Uri.fromFile(image));  

like

       Intent emailIntent = new Intent(android.content.Intent.ACTION_SEND); 
        emailIntent.setType("application/image");
        emailIntent.putExtra(android.content.Intent.EXTRA_EMAIL, new String[]{strEmail}); 
        emailIntent.putExtra(android.content.Intent.EXTRA_SUBJECT,"Test Subject"); 
        emailIntent.putExtra(android.content.Intent.EXTRA_TEXT, "From My App"); 

    File image = new File(Uri.parse("android.resource://" + C.PROJECT_PATH + "/drawable/" + R.drawable.icon_to_share).toString());

       emailIntent.putExtra(Intent.EXTRA_STREAM, Uri.fromFile(image));
        startActivity(Intent.createChooser(emailIntent, "Send mail..."));
raj
  • 2,088
  • 14
  • 23
0

You can see this answer.

First you need get the image in drawable folder

imageName = "planet";
int resourceImage = getActivity().getResources().getIdentifier(imageName, getPackageName());

After you have to save the drawable resource in your internal storage like SDCard.

 try {
            Bitmap bitmap = BitmapFactory.decodeResource(getResources(), resourceImage);
            ByteArrayOutputStream bytes = new ByteArrayOutputStream();
            bitmap.compress(Bitmap.CompressFormat.PNG, 50, bytes);
            File file = new File(Environment.getExternalStorageDirectory() + File.separator + "temporal.png");

            boolean isCreated = file.createNewFile();

            FileOutputStream fileOutputStream = new FileOutputStream(file);
            fileOutputStream.write(bytes.toByteArray());
            fileOutputStream.close();

        } catch (IOException e) {

        }

and finally to send the image via email you have to use an sharedintent:

Intent shareIntent = new Intent(android.content.Intent.ACTION_SEND);
shareIntent.setType("image/png");
shareIntent.putExtra(android.content.Intent.EXTRA_SUBJECT, "this is a subject"); //set your subject
shareIntent.putExtra(android.content.Intent.EXTRA_TEXT, "this is the message"); //set your message

String imagePath = Environment.getExternalStorageDirectory() +File.separator +"temporal.png";
File imageFileToShare = new File(imagePath);
Uri uri = Uri.fromFile(imageFileToShare);
shareIntent.putExtra(Intent.EXTRA_STREAM, uri);
startActivity(Intent.createChooser(shareIntent, "Share image"));
Community
  • 1
  • 1
Robert
  • 1,192
  • 1
  • 10
  • 20