Android: How do I attach a temporary, generated image to an email?

前端 未结 3 1104
盖世英雄少女心
盖世英雄少女心 2020-11-29 02:50

I have a programmatically generated image that I want to send as an attachment via the ACTION_SEND and EXTRA_STREAM method.

But how do i do

3条回答
  •  我在风中等你
    2020-11-29 03:46

    My problem really consisted of two parts:

    1. context.getCacheDir() is private to your app. You can't put something there and expect another app to be able to access it.
    2. I misunderstood what MIME type I should have been using. Even though I was sending email text, I really needed to specify image/png for the sake of my attachment.

    Additionally, research indicated that putting (potentially large) images on the primary memory was not a good idea, even if you were going to immediately clean it up.

    Once I did these things and wrote my generated images to a public location on the SD Card, it worked just fine.

    So, in overview:

    Request SD Card Access in your manifest

    
    

    Make sure SD Card is available

    if (!Environment.MEDIA_MOUNTED.equals(Environment.getExternalStorageState())) 
    {
        //Bail gracefully
    }
    

    Create a directory on the SD Card

    File pngDir = new File(
        Environment.getExternalStorageDirectory(),   
        //Loose convention inferred from app examples
        "Android/data/com.somedomain.someapp/flotsam");
    
    if (!pngDir.exists())
        pngDir.mkdirs();
    

    Write your file to that directory and capture the Uri

    File pngFile = new File(pngDir, "jetsam.png");
    //Save file encoded as PNG
    Uri pngUri = Uri.fromFile(pngFile);
    

    Build an ACTION_SEND intent

    Intent intent = new Intent(android.content.Intent.ACTION_SEND);
    intent.setType("image/png"); //
    intent.putExtra(android.content.Intent.EXTRA_EMAIL, new String[] { "someone@somewhere.com" });
    intent.putExtra(android.content.Intent.EXTRA_SUBJECT, "Portable Network Graphics");
    intent.putExtra(android.content.Intent.EXTRA_CC, new String[] { "carbon@somewhere.com" });
    intent.putExtra(Intent.EXTRA_TEXT, "Something textual");
    intent.putExtra(Intent.EXTRA_STREAM, pngUri);
    

    And then start the activity

    context.startActivity(Intent.createChooser(intent, "Something Pithy"));
    

    And then make sure you clean everything up...

    Caveat 1

    There appears to be more support coming for app-specific SD Card directories, but alas, not in my required SDK version.

    Caveat 2

    This is an overview of the solution that eventually worked for me. It is not necessarily a "best practice" approach.

    Caveat 3

    This does mean that the application has to have an SD Card mounted in order to have the image attachments feature available, but this was totally acceptable for my use case. Your mileage may vary. If the SD Card is not available, I append a friendly note to the email explaining why the images could not be attached and how to rectify the situation.

提交回复
热议问题