https://developer.android.com/reference/android/support/v4/content/FileProvider.html
- Defining a FileProvider
- Specifying Available Files
- Retrieving the Content URI for a File
- Granting Temporary Permissions to a URI
- Serving a Content URI to Another App
<manifest> ... <application> ... <provider android:name="android.support.v4.content.FileProvider" android:authorities="packagename.fileprovider" android:exported="false" android:grantUriPermissions="true"> <meta-data android:name="android.support.FILE_PROVIDER_PATHS" android:resource="@xml/file_paths" /> </provider> ... </application> </manifest> -------------------- xml package in res <?xml version="1.0" encoding="utf-8"?> <paths xmlns:android="http://schemas.android.com/apk/res/android"> <root-path name="my_root" path="" /> <files-path name="my_files" path="" /> <cache-path name="my_cache" path="" /> <external-path name="my_external" path="" /> <external-files-path name="my_files_path" path="files" /> <external-cache-path name="my_cache_path" path="cache" /> </paths>
specify a file
String filename = new SimpleDateFormat("yyyyMMddHHmmss", Locale.CHINA) .format(new Date()) + ".png"; File file = new File(Environment.getExternalStorageDirectory(), filename); mCurrentPhotoPath = file.getAbsolutePath();
retrieve a uri uri format:content://authorities/定义的name属性/文件的相对路径
Uri fileUri =null; if (Build.VERSION.SDK_INT >= 24) { fileUri = FileProvider.getUriForFile(this, "packagename.fileprovider", file); } else { fileUri = Uri.fromFile(file); }
grant a permission
context.grantUriPermission(packageName, fileUri, Intent.FLAG_GRANT_READ_URI_PERMISSION | Intent.FLAG_GRANT_WRITE_URI_PERMISSION); context.revokeUriPermission(Uri uri, int modeFlags); intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION|Intent.FLAG_GRANT_WRITE_URI_PERMISSION);