How to store files in content providers

I have a requirement to attach a zip file to a message in the Android email composer.

The zip file is created by my application and stored in a closed application storage area (accessible via getFilesDirectory ()). I get the URI for this Zip file and add it to the email intent object; when I start email activity, I can see the attachment, but my mail recipient does not receive files,

After doing some research on this, I found that my application data could not be transferred to another application (in this case, the Android email application).

To solve this problem, I would like to apply the recommended solution of using a content provider to share the data of my application.

First of all, I would like to confirm whether this is possible, and can someone please give me some tips on how to do this. I need to know how to copy my Zip file from getFilesDirectory () of my application, the content provider, and how to connect the content provider URI to the email intent object.

I can not put zip files on the SD card ...

I just want to only save my files in my internal storage and connect to the email writer.

+4
source share
2 answers

By default, ContentProvider can be accessed by any application on the device. If you want these files to be accessible to any application on the device, create a ContentProvider , with real implementations for getType() and openFile() . Then URL content:// should work with the AFAIK email application.

Here is an example project demonstrating ContentProvider serving files from local storage, in this case, WebView .

+5
source

If you want to avoid storage on an external SD card, you do not need 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 intention ACTION_SEND.

However, the email program will probably not set the content type.

In addition, there is no reliable way to find out when the email application no longer needs your file. This means that you either run the risk of getting a lot of files, or send new content as an attachment to an older email message.

Another problem with this solution is that everyone can read your zip file. This problem will not occur in the ContentProvider solution, where you can grant permission for access on a β€œfor” basis, that is, allow access to a single file for only one email intent.

Uri matching is then done through a URI, which can start with your package name, for example

 content://com.yourdomain.yourapp.yourproviderclass/some/path 

You can look at http://developer.android.com/guide/topics/manifest/provider-element.html

0
source

All Articles