i have to make an application in which first i have to create a text file from the data user has entered in the UI and then send the text file as an attachment in an email. For this i will have to write data to the external directory and then pass the URI to the Intent so that the email client in the phone can read it.
i am not sure how would i:
write data to the external storage.
2.Pass the URI of the file created to the Intent so that i can attach it to the email.
I have seen several examples of how to send email attachments.. but none fit my requirements hence as a last resort i am posting this question.
thank you in advance.
Both questions have been answered here many times.
External storage:
How can i Use External Storage in android
How should I refer to "external storage" in the UI on Android?
Attach a file to an email:
Trying to attach a file from SD Card to email
Android:Attach file with email from device memory
I honestly cannot figure out what is so specific about your requirements. It looks like a pretty standard task to me...
Related
I have an application that stores data in SQLite db. I've implemented export of this data to external file (in json format) with custom file extension. Also I have an activity that starts when user opens my file and saves it back to SQLite.
I don't want to backup my db, I'm exporting a part of data to be used by another users.
My next goal is to make sharing this data via Bluetooth (export it to Dropbox / send via email). I want it to look like described in this tutorial: http://developer.android.com/training/sharing/send.html but I doubt that this tutorial is what I need.
I don't want users to export data to file and then send this file via Bluetooth, I want them to share data directly from my app.
I looked at FileProvider. I'm confused with insert, update and other methods so I'm not sure this is the right choise.
I looked at Sending Binary Content tutorial but I don't know how to create intent properly and how to save my temporary file for sharing.
Please, describe the way to share this type of data or give me links to examples. Thanks!
P.S. sorry for my English
I have read so many theories about saving a file to the internal storage and external storage that I don't know exactly any more what to do.
I created a PDF file with droidtext that I want to e-mail as an attachment in the chosen e-mail app.
This is no problem. I succeeded in this, however... I only can do it with the external storage.
So, I create a PDF, put it in the external storage with Environment.getExternalStorageDirectory().getAbsolutePath().
However, if no external storage is available I want to save the PDF on the internal storage.
I did it like this so far:
External (working perfectly):
pdf = new File(Environment.getExternalStorageDirectory().getAbsolutePath() + java.io.File.separator + fileName);
PdfWriter.getInstance(document, new FileOutputStream(pdf));
Internal storage:
pdf = new File(fileName);
PdfWriter.getInstance(document, openFileOutput(fileName, MODE_PRIVATE));
With both having Document document = new Document();.
The Internal memory method delivers no error whatsoever, but I am not sure if the file is saved internally.
Also, I think that because it is saved internally, the mail app will not be able to select it as an attachment.
I can't test this because my emulator has no mail app. Nor can I go through the content on the device. Nor do I own a device that has no external storage...
What is the best way to solve this? Force users to have external storage or are there other ways to solve this?
"What is the best way to solve this? Force users to have external storage or are there other ways to solve this?"...I'm not certain, but I think you have no choice but to force users to have external storage to be able to email an attachment.
You can't use MODE_PRIVATE to save to internal storage or the email app won't be able to access the file. You'll have to use MODE_WORLD_READABLE.
To get the directory of where the file is stored use Context.getFilesDir() (http://developer.android.com/reference/android/content/Context.html#getFilesDir%28%29)
You can also just install an email app on the emulator to test this. Just download an apk and install it via adb.
In my application I allow users to share files with external apps by using content provider. My files are encrypted so before I can share one of those files, I need to decrypt it and store it in application internal storage. After that I implemented custom content provider that can point to the file by using a given URL and return ParcelFileDescriptor instance.
For security issues I would like to remove the file from internal storage after external application has used it, or user canceled operation.
EDIT: The external application that I am sharing file with is a 3rd party application.
So my question is:
When should I remove the file, or do you know of any event that I can listen to or a handler that I can attach to the file?
Thank you for all you responses.
Side question would be:
Do you know of a way where I wouldn't have to store the file on internal storage, but rather send it directly as a stream to the external app?
The answer to my question is to use pipes ParcelFileDescriptor.
Code is described in The Busy Coder's Guide to Android Development from CommonsWare.
The solution is based on:
https://github.com/nandeeshwar/Pfd-Create-Pipe
Actual solution that I managed to implement is:
https://github.com/commonsguy/cw-omnibus/tree/master/ContentProvider/Pipe
If it is your external application, you could leave an encrypted file and pass the key in an Intent. If it is a 3-rd party external application, you have to find out what that application can accept.
You might also read about services -- maybe they provide what you want.
As to "when", I'd suggest launching a clean-up task in onResume(). (Unless you decide to use a service.)
How can I implement ability to attach files from file system to my application. Can I use some libraries, or I should do it myself programmatically? Or I can implement code of other similar application and use them?
File browser:
Link
A sample code for file browser. Or you can just let user input the location into an EditText and let app remember it.
To use external storage(like SD card), please see here
Remember the file location:
Using Shared Preferences
It is a simpler way to save persistent data than SQLite database
Send it to server:
Link
Here is an example of how to use HTTP POST to send to server
I have a requirement to attach a Zip file to a message in the Android email composer.
The Zip file is being created by my application and stored in the app's private storage area (accessible via getFilesDirectory()).
I am obtaining the URI to this Zip file and appending it to an email intent object; when I launch the email activity I am able see the attachment but my email recipient is not receiving the files,
After doing some research on this, I found that my application data cannot be shared with other app's (in this case the Android Email app).
To solve this problem I would like to implement the recommend solution of using a content provider to share my application data.
First of all, I would like to confirm if this is possible, and if it is could anyone please give me some hints on how to go about doing this. I need to know how to copy my Zip file from the getFilesDirectory() of my application, to the content provider, and also how to attach the content provider URI to the email intent object.
I can't place my zip files into an SD card...
I just want only to store my files in to my device internal storage and attach to the email composer.
By default, a ContentProvider can be accessed by any application on the device. If you are willing for these files to be accessed by any application on the device, create a ContentProvider, with real implementations for getType() and openFile(). Then, the content:// URL should work with the Email app, AFAIK.
Here is a sample project demonstrating a ContentProvider serving up files from local storage, in this case to a WebView.
If all you want is to avoid storage on the external SD card, then you don't need a ContentProvider. You can live with
openFileOutput("yourzipfile.zip", MODE_WORLD_READABLE)
and pass
putExtra(Intent.EXTRA_STREAM, Uri.fromFile(getFileStreamPath("yourzipfile.zip))
along with your ACTION_SEND Intent.
However, the email program will probably set no Content-type.
Also, there's no reliable way to tell when the email app doesn't need your file anymore. This means that you either risk ending up with many files or sending a newer content as the attachment of an older email.
Another issue with this solution is that everyone can read your zip file. This problem would not occur with a ContentProvider solution, where you can grant access permission on a per-Intent basis, i.e. allowing access to one file only for one email Intent.
Matching of the Uri is then done via the URI, which may start with your package name, such as
content://com.yourdomain.yourapp.yourproviderclass/some/path
You may want to look at http://developer.android.com/guide/topics/manifest/provider-element.html