您的位置:首页 > 移动开发 > Android开发

Google Android开发者文档系列-创建有内容分享特性的应用之设置共享文件

2016-06-06 15:23 776 查看

Setting Up File Sharing(设置共享文件)

该系列文章是我在学习Google开发者文档时结合谷歌翻译和自身理解编写的,希望对学习Android开发的朋友带来些便利,由于个人翻译水平有限,所以内容包含原文和译文,希望浏览者结合理解,以免步入我可能错译的误区。在此感谢http://android.xsoftlab.net/提供的镜像,希望转载者注明出处/article/11851915.html方便查看最新博客

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 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 FileProvider to your app, and how to specify the files you want to offer to other apps.

为了安全的从你的app提供一个文件到另一个app,你需要配置你的app以content URI的形式为文件提供一个安全的句柄。而Android FileProvider组件会根据您在XML提供的规格来生成文件的content URI。本课将向您展示如何在你的app中添加默认的FileProvider实现,以及如何指定要提供给其他应用程序的文件。

Note: The FileProvider class is part of the v4 Support Library. For information about including this library in your application, see Support Library Setup.

注:FileProvider 类是V4 Support Library的一部分,了解更多关于在你的app中包含该library的内容,查看Support Library Setup。

Specify the FileProvider(指定FileProvider)

Defining a 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.

为你的app指定一个FileProvider需要在你的清单文件中定义一个入口。这个入口定义指定产生content URI时需要的权限,以及指定你的app可以共享路径的XML文件的名称。

The following snippet shows you how to add to your manifest the element that specifies the FileProvider class, the authority, and the XML file name:

下面的代码片段展示了如何向你的清单中的要素下添加指定的FileProvider类、权限,和XML文件名:

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


In this example, the android:authorities attribute specifies the URI authority that you want to use for content URIs generated by the FileProvider. In the example, the authority is com.example.myapp.fileprovider. For your own app, specify an authority consisting of the app’s android:package value with the string “fileprovider” appended to it. To learn more about the authority value, see the topic Content URIs and the documentation for the android:authorities attribute.

在这个例子中,android:authorities属性指定了要使用FileProvider生成content URI时需要的URI权限。在本例中,权限是com.example.myapp.fileprovider。在你自己的应用程序中,指定应用程序的权限:该权限由app的android:package值和追加到包名后面的”fileprovider”字符串组成。要了解更多关于权限值的内容,请参阅Content URIs主题和android:authorities属性的文档。

The <?(去掉问号)meta-data> child element of the points to an XML file that specifies the directories you want to share. The android:resource attribute is the path and name of the file, without the .xml extension.The contents of this file are described in the next section.

这个<?(去掉问号)provider>的<?(去掉问号)meta-data>子要素指向一个定义了你想共享的文件路径的xml文件。android:resource:属性是该文件不包括.xml扩展名的路径和名字。该文件的内容,将在下一节中描述。

Specify Sharable Directories(指定共享的目录)

Once you have added the 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 filepaths.xml in the res/xml/ 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 res/xml/filepaths.xml. The snippet also demonstrates how to share a subdirectory of the files/ directory in your internal storage area:

一旦你已经在应用清单中添加了FileProvider,则需要指定您要共享的文件的路径。要指定路径,先要在项目的res / xml /子目录下创建filepaths.xml。在这个文件中,通过添加XML要素来指定每个路径。下面的代码片段显示了您的res / xml / filepaths.xml的内容的例子。该段代码还演示了如何共享您的内部存储区域中的files/目录的子目录:

<paths>
<files-path path="images/" name="myimages" />
</paths>


In this example, the <?(去掉问号)files-path> tag shares directories within the files/ directory of your app’s internal storage. The path attribute shares the images/ subdirectory of files/. The name attribute tells the FileProvider to add the path segment myimages to content URIs for files in the files/images/ subdirectory.

在这个例子中,<?(去掉问号)files-path>标签共享了你的app的内部存储的files/ 下的路径。path属性共享files/下的images/ 子目录。name属性告诉FileProvider在files/images/子目录下添加content URI的myimages路径段。

The <?(去掉问号)paths> element can have multiple children, each specifying a different directory to share. In addition to the <?(去掉问号)files-path> element, you can use the <?(去掉问号)external-path> element to share directories in external storage, and the <?(去掉问号)cache-path> element to share directories in your internal cache directory. To learn more about the child elements that specify shared directories, see the FileProvider reference documentation.

<?(去掉问号)paths>要素可以包含多个子要素,它们分别定义一个不同的共享路径。除了<?(去掉问号)files-path>要素,你可以使用<?(去掉问号)external-path>要素来共享外部存储共享路径和<?(去掉问号)cache-path>要素来在你的内部缓存路径中共享路径。要了解更多关于指定共享目录的子元素的内容,请参阅FileProvider参考文档。

Note: The XML file is the only way you can specify the directories you want to share; you can’t programmatically add a directory.

注:xml文件是你定义你想共享路径的唯一方法,你不能以编程方式添加目录。

You now have a complete specification of a FileProvider that generates content URIs for files in the files/ directory of your app’s internal storage or for files in subdirectories of files/. When your app generates a content URI for a file, it contains the authority specified in the <?(去掉问号)provider> element (com.example.myapp.fileprovider), the path myimages/, and the name of the file.

你现在有一个完整的FileProvider定义了,它生成你的app内部存储下files/路径或其子路径下对应的content URI。但你的app产生一个文件的content URI时,它会包含在 <?(去掉问号)provider>要素中定义好的权限(com.example.myapp.fileprovider)、路径myimages/和文件的名称。

For example, if you define a FileProvider according to the snippets in this lesson, and you request a content URI for the file default_image.jpg, FileProvider returns the following URI:

例如,如果你根据下面的代码片段定义一个FileProvider,并且请求default_image.jpg文件对应的content URI ,FileProvider返回如下URI:

content://com.example.myapp.fileprovider/myimages/default_image.jpg
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: