145 lines
7.0 KiB
Plaintext
145 lines
7.0 KiB
Plaintext
page.title=Setting Up File Sharing
|
|
trainingnavtop=true
|
|
@jd:body
|
|
|
|
|
|
<div id="tb-wrapper">
|
|
<div id="tb">
|
|
|
|
<!-- table of contents -->
|
|
<h2>This lesson teaches you to</h2>
|
|
<ol>
|
|
<li><a href="#DefineProvider">Specify the FileProvider</a></li>
|
|
<li><a href="#DefineMetaData">Specify Sharable Directories</a></li>
|
|
</ol>
|
|
|
|
<h2>You should also read</h2>
|
|
<ul>
|
|
<li><a href="{@docRoot}guide/topics/data/data-storage.html">Storage Options</a></li>
|
|
<li><a href="{@docRoot}training/basics/data-storage/files.html">Saving Files</a>
|
|
</ul>
|
|
|
|
</div>
|
|
</div>
|
|
|
|
<p>
|
|
To securely offer a file from your app to another app, you need to configure your app to offer
|
|
a secure handle to the file, in the form of a content URI. The Android
|
|
{@link android.support.v4.content.FileProvider} component generates content URIs for
|
|
files, based on specifications you provide in XML. This lesson shows you how to add the default
|
|
implementation of {@link android.support.v4.content.FileProvider} to your app, and how to
|
|
specify the files you want to offer to other apps.
|
|
</p>
|
|
|
|
<p class="note">
|
|
<strong>Note:</strong> The {@link android.support.v4.content.FileProvider} class is part of the
|
|
<a href="{@docRoot}tools/support-library/features.html#v4">v4 Support Library</a>. For information
|
|
about including this library in your application, see
|
|
<a href="{@docRoot}tools/support-library/setup.html">Support Library Setup</a>.
|
|
</p>
|
|
|
|
<h2 id="DefineProvider">Specify the FileProvider</h2>
|
|
<p>
|
|
Defining a {@link android.support.v4.content.FileProvider} for your app requires an entry in
|
|
your manifest. This entry specifies the authority to use in generating content URIs, as well as
|
|
the name of an XML file that specifies the directories your app can share.
|
|
</p>
|
|
<p>
|
|
The following snippet shows you how to add to your manifest the
|
|
<code><a href="{@docRoot}guide/topics/manifest/provider-element.html"
|
|
><provider></a></code> element that specifies the
|
|
{@link android.support.v4.content.FileProvider} class, the authority, and the
|
|
XML file name:
|
|
</p>
|
|
<pre>
|
|
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
|
|
package="com.example.myapp">
|
|
<application
|
|
...>
|
|
<provider
|
|
android:name="android.support.v4.content.FileProvider"
|
|
android:authorities="com.example.myapp.fileprovider"
|
|
android:grantUriPermissions="true"
|
|
android:exported="false">
|
|
<meta-data
|
|
android:name="android.support.FILE_PROVIDER_PATHS"
|
|
android:resource="@xml/filepaths" />
|
|
</provider>
|
|
...
|
|
</application>
|
|
</manifest></pre>
|
|
<p>
|
|
In this example, the <code><a href="{@docRoot}guide/topics/manifest/provider-element.html#auth"
|
|
>android:authorities</a></code> attribute specifies the URI authority
|
|
that you want to use for content URIs generated by the
|
|
{@link android.support.v4.content.FileProvider}.
|
|
In the example, the authority is <code>com.example.myapp.fileprovider</code>. For your own
|
|
app, specify an authority consisting of the app's
|
|
<code><a href="{@docRoot}guide/topics/manifest/manifest-element.html#package"
|
|
>android:package</a></code> value with the string "fileprovider" appended to it. To learn more
|
|
about the authority value, see the topic
|
|
<a href="{@docRoot}guide/topics/providers/content-provider-basics.html#ContentURIs"
|
|
>Content URIs</a> and the documentation for the
|
|
<code><a href="{@docRoot}guide/topics/manifest/provider-element.html#auth"
|
|
>android:authorities</a></code> attribute.
|
|
</p>
|
|
<p>
|
|
The <code><a href="{@docRoot}guide/topics/manifest/meta-data-element.html"
|
|
><meta-data></a></code> child element of the
|
|
<code><a href="{@docRoot}guide/topics/manifest/provider-element.html"
|
|
><provider></a></code> points to an XML file that specifies the directories you want to
|
|
share. The <code>android:resource</code> attribute is the path and name of the file, without
|
|
the <code>.xml</code> extension.The contents of this file are described in the next section.
|
|
</p>
|
|
<h2 id="DefineMetaData">Specify Sharable Directories</h2>
|
|
<p>
|
|
Once you have added the {@link android.support.v4.content.FileProvider} to your app manifest,
|
|
you need to specify the directories that contain the files you want to share. To specify the
|
|
directories, start by creating the file <code>filepaths.xml</code> in the <code>res/xml/</code>
|
|
subdirectory of your project. In this file, specify the directories by adding an XML element for
|
|
each directory. The following snippet shows you an example of the contents of
|
|
<code>res/xml/filepaths.xml</code>. The snippet also demonstrates how to share a subdirectory
|
|
of the <code>files/</code> directory in your internal storage area:
|
|
</p>
|
|
<pre>
|
|
<paths>
|
|
<files-path path="images/" name="myimages" />
|
|
</paths></pre>
|
|
<p>
|
|
In this example, the <code><files-path></code> tag shares directories within the
|
|
<code>files/</code> directory of your app's internal storage. The <code>path</code> attribute
|
|
shares the <code>images/</code> subdirectory of <code>files/</code>. The <code>name</code>
|
|
attribute tells the {@link android.support.v4.content.FileProvider} to add the path segment
|
|
<code>myimages</code> to content URIs for files in the <code>files/images/</code> subdirectory.
|
|
</p>
|
|
<p>
|
|
The <code><paths></code> element can have multiple children, each specifying a different
|
|
directory to share. In addition to the <code><files-path></code> element, you can
|
|
use the <code><external-path></code> element to share directories in external storage, and
|
|
the <code><cache-path></code> element to share directories in your internal cache
|
|
directory. To learn more about the child elements that specify shared directories, see the
|
|
{@link android.support.v4.content.FileProvider} reference documentation.
|
|
</p>
|
|
<p class="note">
|
|
<strong>Note:</strong> The XML file is the only way you can specify the directories you want to
|
|
share; you can't programmatically add a directory.
|
|
</p>
|
|
<p>
|
|
You now have a complete specification of a {@link android.support.v4.content.FileProvider}
|
|
that generates content URIs for files in the <code>files/</code> directory of your app's
|
|
internal storage or for files in subdirectories of <code>files/</code>. When your app generates
|
|
a content URI for a file, it contains the authority specified in the
|
|
<code><a href="{@docRoot}guide/topics/manifest/provider-element.html"
|
|
><provider></a></code> element (<code>com.example.myapp.fileprovider</code>),
|
|
the path <code>myimages/</code>, and the name of the file.
|
|
</p>
|
|
<p>
|
|
For example, if you define a {@link android.support.v4.content.FileProvider} according to the
|
|
snippets in this lesson, and you request a content URI for the file
|
|
<code>default_image.jpg</code>, {@link android.support.v4.content.FileProvider} returns the
|
|
following URI:
|
|
</p>
|
|
<pre>
|
|
content://com.example.myapp.fileprovider/myimages/default_image.jpg</pre>
|
|
|