Android 7.0文件共享
2019-05-28 本文已影响0人
年华_零落成诗
1.指定一个FileProvider
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.myapp">
<application>
<provider
android:name="android.support.v4.content.FileProvider"
//标识,建议用“${applicationId}.fileprovider”命名
android:authorities="com.example.myapp.fileprovider"
android:grantUriPermissions="true"
android:exported="false">
<meta-data
//文件共享目录,位于res/xml/目录下
android:name="android.support.FILE_PROVIDER_PATHS"
android:resource="@xml/filepaths" />
</provider>
</application>
</manifest>
可以创建一个类继承v4包下的FileProvider:
/**
不用实现任何方法
*/
public class MyFileProvider extends FileProvider{
}
2.指定共享路径
在res/xml/目录下创建一个名为filepaths.xml的文件:
<paths>
//path:要访问的路径
//name:名称,生成uri时使用
<files-path path = “images /” name = “myimages” />
</paths>
files-path指的是应用程序内部存储的目录,比如我们要获取该目录下名为default_image.jpg的文件,生成的uri为:
content://com.example.myapp.fileprovider/myimages/default_image.jpg
3.开始使用
调用系统相机拍照:
private Intent requestFileIntent;
File requestFile = new File(filePath);
requestFileIntent = new Intent(Intent.ACTION_VIEW);
requestFileIntent.setType("image/jpg");
//构造uri
Uri fileUri=FileProvider.getUriForFile(this,"com.example.myapp.fileprovider",requestFile);
//授予权限
requestFileIntent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION | Intent.FLAG_GRANT_WRITE_URI_PERMISSION);
//保存位置
intent.putExtra(MediaStore.EXTRA_OUTPUT, uri);
startActivityForResult(requestFileIntent, 0);
4.paths说明与目录对应关系:
1. 说明:
- path:目录名称,不能为空,可以为“.或者/”
- name:生成uri需要,不能为空
path是指当前目录下的子文件夹,“.“或者”/”时代表根目录,”images“代表该目录下 images文件夹
2.格式:
<paths xmlns:android = “http://schemas.android.com/apk/res/android” >
<files-path name = “my_images” path = “images /” />
<files-path name = “my_docs” path = “docs /” />
</paths>
3. 对应关系:
| 命名 | 目录 | 说明 | 路径 |
|---|---|---|---|
| <files-path/> | Context.getFilesDir() | 应用内部存储目录 | /data/data/<application package>/files |
| <cache-path/> | getCacheDir() | 应用内部缓存目录 | /data/data/<application package>/cache |
| <external-path/> | Environment.getExternalStorageDirectory() | 外部存储目录 | /sdcard/ |
| <external-files-path/> | Context.getExternalFilesDir(String) | 应用外部存储目录 | SDCard/Android/data/<application package>/files/ |
| <external-cache-path/> | Context.getExternalCacheDir() | 应用外部缓存目录 | SDCard/Android/data/<application package>/cache/ |
| <external-media-path/> | Context.getExternalMediaDirs() | 应用外部媒体目录(api>=21) |
MIME Type 对照表
| 格式 | mimetype |
|---|---|
| .3gp | video/3gpp |
| .apk | application/vnd.android.package-archive |
| .asf | video/x-ms-asf |
| .avi | video/x-msvideo |
| .bin | application/octet-stream |
| .bmp | image/bmp |
| .c | text/plain |
| .cla | application/octet-stream |
| .con | text/plain |
| .cpp | text/plain |
| .doc | application/msword |
| .doc | application/vnd.openxmlformats-officedocument.wordprocessingml.document |
| .xls | application/vnd.ms-excel |
| .xls | application/vnd.openxmlformats-officedocument.spreadsheetml.sheet |
| .exe | application/octet-stream |
| .gif | image/gif |
| .gta | application/x-gtar |
| .gz | application/x-gzip |
| .h | text/plain |
| .htm | text/html |
| .htm | text/html |
| .jar | application/java-archive |
| .jav | text/plain |
| .jpe | image/jpeg |
| .jpg | image/jpeg |
| .js | application/x-javascript |
| .log | text/plain |
| .m3u | audio/x-mpegurl |
| .m4a | audio/mp4a-latm |
| .m4b | audio/mp4a-latm |
| .m4p | audio/mp4a-latm |
| .m4u | video/vnd.mpegurl |
| .m4v | video/x-m4v |
| .mov | video/quicktime |
| .mp2 | audio/x-mpeg |
| .mp3 | audio/x-mpeg |
| .mp4 | video/mp4 |
| .mpc | application/vnd.mpohun.certificate |
| .mpe | video/mpeg |
| .mpe | video/mpeg |
| .mpg | video/mpeg |
| .mpg | video/mp4 |
| .mpg | audio/mpeg |
| .msg | application/vnd.ms-outlook |
| .ogg | audio/ogg |
| application/pdf | |
| .png | image/png |
| .pps | application/vnd.ms-powerpoint |
| .ppt | application/vnd.ms-powerpoint |
| .ppt | application/vnd.openxmlformats-officedocument.presentationml.presentation |
| .pro | text/plain |
| .rc | text/plain |
| .rmv | audio/x-pn-realaudio |
| .rtf | application/rtf |
| .sh | text/plain |
| .tar | application/x-tar |
| .tgz | application/x-compressed |
| .txt | text/plain |
| .wav | audio/x-wav |
| .wma | audio/x-ms-wma |
| .wmv | audio/x-ms-wmv |
| .wps | application/vnd.ms-works |
| .xml | text/plain |
| .z | application/x-compress |
| .zip | application/x-zip-compressed |
| " " | */* |
参考