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 8036459
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T02:37:38+00:00 2026-06-05T02:37:38+00:00

The final objective will be clear shortly. I want to create a file object

  • 0

The final objective will be clear shortly.

I want to create a file object and instead of getting data from a real physical file I want to provide the buffer myself.

Then, I want to use this file, which does not really exist in the sdcard or anywhere outside my app, give it a name and send it by email as an attachment (using the EXTRA_STREAM).

I found the following bit of code, by Adriaan Koster (@adriaankoster), the post Write byte[] to File in Java

// convert byte[] to File
ByteArrayInputStream bis = new ByteArrayInputStream(bytes);
ObjectInputStream ois = new ObjectInputStream(bis);
File fileFromBytes = (File) ois.readObject();
bis.close();
ois.close();

System.out.println(fileFromBytes);

I used it to create this function

private File fileFromBytes(byte[] buf) {
    File f = null;
    try {
        ByteArrayInputStream bis = new ByteArrayInputStream(buf);
        ObjectInputStream ois = new ObjectInputStream(bis);
        f = (File) ois.readObject();
        bis.close();
        ois.close();
    }
    catch (Exception e) {}
    return f;
}

and here is where I am stuck, because when I use it:

// When sent as body the mail is sent OK
// emailIntent.putExtra(android.content.Intent.EXTRA_TEXT, dump());

// When I try to attach the mail is empty
emailIntent.putExtra(android.content.Intent.EXTRA_STREAM, fileFromBytes(dump().getBytes()));

I know from examples I’ve seen the second argument should be an URI, but: How do I create a virtual URI to fit my file?

EDIT:
The option to attach data directly from within the application is important to certain kind of applications. Namely, security & banking applications that do not want to move sensitive data around too much. Surely if the data does not reach the sdcard and goes directly to a mail attachment it is harder to sniff than within the application memory.
This is not my specific case, but I wanted to point out that this capability is important to have.

  • 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-06-05T02:37:40+00:00Added an answer on June 5, 2026 at 2:37 am

    The first thing you’ll want to do, I imagine, is create a ContentProvider. You can see an example implementation here

    https://github.com/dskinner/AndroidWeb/blob/master/src/org/tsg/web/WebContentProvider.java

    where in the above link’s case, you would add this to your AndroidManifest.xml

    <provider
        android:name="org.tsg.web.WebContentProvider"
        android:authorities="your.package.name" />
    

    Now, you’ll have a content uri available for use, content://your.package.name/.

    The portion of the above ContentProvider your interested in, again I imagine, is the openFile method. When sharing data by intent across apps, certain things are expected. In your case, you’re looking to share some byte data that’s meant to be attached to the email.

    So if you pass in a content uri to the email app such as content://your.package.name/foo with the appropriate intent flags, then openFile will get called on your ContentProvider. In this case, you can inspect the end of the uri segment to see foo was requested, and return appropriately.

    The next issue you bring up is not having the file actually on disk. While I can’t vouch for the method you used above (though it looks kosher), what you need to be returning is a ParcelFileDescriptor from your ContentProvider. If you look at the link I provided, you could possibly try to use that as a sample to get the file descriptor from your File object (my knowledge waivers here), but I imagine, the data simply wont be available at that point.

    What you do bring up is security though. It’s important to note that you can write data to disk privately so only the app has access to the data. I believe, but you might want to double check on this, if that data is private to the app, you can expose it via the ContentProvider and possibly lock down who and how the provider gets used, who can call it, etc. You may want to dig into android docs for that portion or look at some other SO questions.

    Anyway, good luck.

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

Sidebar

Related Questions

Final Edit: I was able to locate the duplicate field in the ini file.
final Object o; List l = new ArrayList(){{ // closure over o, in lexical
The final images produced by compliers contain both bin file and extended loader format
I'm currently writing an iPhone application which gets some data from the user and
Objective I want to be able to run the Applescript to open multiple applications
My installer places a file into /Library/Application Support/AppName. As a final step of the
I am doing the final touch ups on an app and I am getting
final CharSequence[] items = {Red, Green, Blue} AlertDialog.Builder builder = new AlertDialog.Builder(this); builder.setTitle(Pick a
final TableLayout table = (TableLayout) findViewById(R.id.tableLayout); TableRow row = new TableRow(this); TextView t2 =
final HttpClient hClient = new DefaultHttpClient(); final HttpPost hPost = new HttpPost( http://xxx.xxx); try

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.