FileProvider android 7 文件共享
2016-10-11 11:34
441 查看
翻译成人话比较难,还是尝试阅读原版吧!
FileProvider is a special subclass of
sharing of files associated with an app by creating a
a file instead of a
A content URI allows you to grant read and write access using temporary access permissions. When you create an
a content URI, in order to send the content URI to a client app, you can also call
add permissions. These permissions are available to the client app for as long as the stack for a receiving
active. For an
the permissions are available as long as the
In comparison, to control access to a
file system permissions of the underlying file. The permissions you provide become available to any app, and remain in effect until you change them. This level of access is fundamentally insecure.
The increased level of file access security offered by a content URI makes FileProvider a key part of Android's security infrastructure.
itself, add a
to
use the authority
to
If you want to override any of the default behavior of FileProvider methods, extend the FileProvider class and use the fully-qualified class name in the
For example, the following
of your private file area.
Put the
as a child of the
attribute to
the file, then pass the
You can send the content URI returned by
another app in an
and access its contents by calling
get a
For example, suppose your app is offering files to other apps with a FileProvider that has the authority
of your internal storage add the following code:
As a result of the previous snippet,
the content URI
do one of the following:
Call the method
mode flags. This grants temporary access permission for the content URI to the specified package, according to the value of the the
both. The permission remains in effect until you revoke it by calling
until the device reboots.
Put the content URI in an
Next, call the method
either
both.
Finally, send the
do this by calling
Permissions granted in an
effect while the stack of the receiving
permissions are automatically removed. Permissions granted to one
app are automatically extended to other components of that app.
which sends an
your app. In response, your app can immediately return a content URI to the client app or present a user interface that allows the user to pick a file. In the latter case, once the user picks the file your app can return its content URI. In both cases, your
app returns the content URI in an
You can also put the content URI in a
send to a client app. To do this, call
When you use this approach, you can add multiple
each with its own content URI. When you call
set temporary access permissions, the same permissions are applied to all of the content URIs.
Note: The
is only available in platform version 16 (Android 4.1) and later. If you want to maintain compatibility with previous versions, you should send one content URI at a time in the
Set the action to
FileProvider is a special subclass of
ContentProviderthat facilitates secure
sharing of files associated with an app by creating a
content://
Urifor
a file instead of a
file:///
Uri.
A content URI allows you to grant read and write access using temporary access permissions. When you create an
Intentcontaining
a content URI, in order to send the content URI to a client app, you can also call
Intent.setFlags()to
add permissions. These permissions are available to the client app for as long as the stack for a receiving
Activityis
active. For an
Intentgoing to a
Service,
the permissions are available as long as the
Serviceis running.
In comparison, to control access to a
file:///
Uriyou have to modify the
file system permissions of the underlying file. The permissions you provide become available to any app, and remain in effect until you change them. This level of access is fundamentally insecure.
The increased level of file access security offered by a content URI makes FileProvider a key part of Android's security infrastructure.
Defining a FileProvider
Since the default functionality of FileProvider includes content URI generation for files, you don't need to define a subclass in code. Instead, you can include a FileProvider in your app by specifying it entirely in XML. To specify the FileProvider componentitself, add a
<provider>element to your app manifest. Set the
android:nameattribute
to
android.support.v4.content.FileProvider. Set the
android:authoritiesattribute to a URI authority based on a domain you control; for example, if you control the domain
mydomain.comyou should
use the authority
com.mydomain.fileprovider. Set the
android:exportedattribute to
false; the FileProvider does not need to be public. Set the android:grantUriPermissions attribute
to
true, to allow you to grant temporary access to files. For example:
<manifest> ... <application> ... <provider android:name="android.support.v4.content.FileProvider" android:authorities="com.mydomain.fileprovider" android:exported="false" android:grantUriPermissions="true"> ... </provider> ... </application> </manifest>
If you want to override any of the default behavior of FileProvider methods, extend the FileProvider class and use the fully-qualified class name in the
android:nameattribute of the
<provider>element.
Specifying Available Files
A FileProvider can only generate a content URI for files in directories that you specify beforehand. To specify a directory, specify the its storage area and path in XML, using child elements of the<paths>element.
For example, the following
pathselement tells FileProvider that you intend to request content URIs for the
images/subdirectory
of your private file area.
<paths xmlns:android="http://schemas.android.com/apk/res/android"> <files-path name="my_images" path="images/"/> ... </paths>
Put the
<paths>element and its children in an XML file in your project. For example, you can add them to a new file called
res/xml/file_paths.xml. To link this file to the FileProvider, add a <meta-data> element
as a child of the
<provider>element that defines the FileProvider. Set the
<meta-data>element's "android:name" attribute to
android.support.FILE_PROVIDER_PATHS. Set the element's "android:resource"
attribute to
@xml/file_paths(notice that you don't specify the
.xmlextension). For example:
<provider android:name="android.support.v4.content.FileProvider" android:authorities="com.mydomain.fileprovider" android:exported="false" android:grantUriPermissions="true"> <meta-data android:name="android.support.FILE_PROVIDER_PATHS" android:resource="@xml/file_paths" /> </provider>
Generating the Content URI for a File
To share a file with another app using a content URI, your app has to generate the content URI. To generate the content URI, create a newFilefor
the file, then pass the
Fileto
getUriForFile().
You can send the content URI returned by
getUriForFile()to
another app in an
Intent. The client app that receives the content URI can open the file
and access its contents by calling
ContentResolver.openFileDescriptorto
get a
ParcelFileDescriptor.
For example, suppose your app is offering files to other apps with a FileProvider that has the authority
com.mydomain.fileprovider. To get a content URI for the file
default_image.jpgin the
images/subdirectory
of your internal storage add the following code:
File imagePath = new File(Context.getFilesDir(), "images"); File newFile = new File(imagePath, "default_image.jpg"); Uri contentUri = getUriForFile(getContext(), "com.mydomain.fileprovider", newFile);
As a result of the previous snippet,
getUriForFile()returns
the content URI
content://com.mydomain.fileprovider/my_images/default_image.jpg.
Granting Temporary Permissions to a URI
To grant an access permission to a content URI returned fromgetUriForFile(),
do one of the following:
Call the method
Context.grantUriPermission(package, Uri, mode_flags)for the
content://
Uri, using the desired
mode flags. This grants temporary access permission for the content URI to the specified package, according to the value of the the
mode_flagsparameter, which you can set to
FLAG_GRANT_READ_URI_PERMISSION,
FLAG_GRANT_WRITE_URI_PERMISSIONor
both. The permission remains in effect until you revoke it by calling
revokeUriPermission()or
until the device reboots.
Put the content URI in an
Intentby calling
setData().
Next, call the method
Intent.setFlags()with
either
FLAG_GRANT_READ_URI_PERMISSIONor
FLAG_GRANT_WRITE_URI_PERMISSIONor
both.
Finally, send the
Intentto another app. Most often, you
do this by calling
setResult().
Permissions granted in an
Intentremain in
effect while the stack of the receiving
Activityis active. When the stack finishes, the
permissions are automatically removed. Permissions granted to one
Activityin a client
app are automatically extended to other components of that app.
Serving a Content URI to Another App
There are a variety of ways to serve the content URI for a file to a client app. One common way is for the client app to start your app by callingstartActivityResult(),
which sends an
Intentto your app to start an
Activityin
your app. In response, your app can immediately return a content URI to the client app or present a user interface that allows the user to pick a file. In the latter case, once the user picks the file your app can return its content URI. In both cases, your
app returns the content URI in an
Intentsent via
setResult().
You can also put the content URI in a
ClipDataobject and then add the object to an
Intentyou
send to a client app. To do this, call
Intent.setClipData().
When you use this approach, you can add multiple
ClipDataobjects to the
Intent,
each with its own content URI. When you call
Intent.setFlags()on the
Intentto
set temporary access permissions, the same permissions are applied to all of the content URIs.
Note: The
Intent.setClipData()method
is only available in platform version 16 (Android 4.1) and later. If you want to maintain compatibility with previous versions, you should send one content URI at a time in the
Intent.
Set the action to
ACTION_SENDand put the URI in data by calling
setData().
相关文章推荐
- Android 应用间共享文件(FileProvider)
- Android 7.0 行为变更 通过FileProvider在应用间共享文件
- Android 7.0 行为变更 通过FileProvider在应用间共享文件吧
- Android 7.0配置fileprovider共享文件 解决FileUriExposedException
- Android 7.0 行为变更 通过FileProvider在应用间共享文件
- Android 7.0 通过FileProvider实现应用间文件共享
- Android N 7.0 应用间共享文件(FileProvider)
- Android实践 -- 使用FileProvider安全的共享文件 简介篇
- Android 7.0 行为变更 通过FileProvider 在应用间共享文件
- android 7.0 使用FileProvider在应用间共享文件(相机适配)
- Android 7.0 适配-应用之间共享文件(FileProvider)
- Android实践 -- 使用FileProvider安全的共享文件 应用篇
- 使用FileProvider共享文件
- Android7.0 FileProvider文件共享等安全及兼容性
- Android7.0须知--应用间共享文件(FileProvider)
- Android 7.0 行为变更 通过FileProvider在应用间共享文件吧
- Android7.0须知--应用间共享文件(FileProvider)
- 【随心笔录】Android N 文件Uri传递,报出FileUriExposedException异常,使用FileProvider
- Android 7.0 行为变更 通过FileProvider在应用间共享文件吧
- Android 7.0 应用间文件访问方法(FileProvider)