Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 1039511
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T15:06:32+00:00 2026-05-16T15:06:32+00:00

I have a programmatically generated image that I want to send as an attachment

  • 0

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 this?

My first attempt (writing to my context.getCacheDir() based file path) appeared to work in the Gmail preview (no image preview, but attached file name and icon was visible), but the attachment never arrived on the recipient side. I guess this has something to do with permissions on the generated file, but how to avoid this? Do I need to set more permissive settings on these generated files (so that the Gmail activity can access)? Is that even possible for the app’s cache folder?

Is there another file location that would be more suitable to write my files to? I considered the downloads folder, but think it would be an awkward location for something that only needs to exist until it has been emailed.

I have even tried encoding my image purely in a data:image/png;base64,ABCD... style URI. This, too, showed up in Gmail preview (attachment icon, but no file name), but did not result in a recipient-side attachment.

Has anyone been able to attach a one-shot generated image to an email intent by any means? What options may I have overlooked?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-16T15:06:32+00:00Added an answer on May 16, 2026 at 3:06 pm

    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

    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
    

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a certificate generated via MakeCert. I want to use this certificate for
I have calculated that if I want my generated image to be A4 size
I have a clickable linear layout that I've generated programmaticly, and I want it
I have a system that emails programmatically generated emails hundreds of times daily. They
I want to programmatically download the contents of a web page, but that page
I have 9 (programmatically generated) buttons. I do this for all of them: Button
I have a JEditorPane that displays HTML that is generated programmatically (at runtime). Up
I have a radiogroup in xml, and the buttons are generated programmatically. How do
Would you call this implementation of a multiton in objective-c 'elegant'? I have programmatically
I have UIScrollView which i populate with UIImageViews programmatically. After that I change contentSize

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.