通过FileProvider.getUriForFile(@NonNull Context context, @NonNull String authority, @NonNull File file)方法获得一个有临时权限的Uri给客户端用来访问本APP文件。
当然看FileProvider类的注释更加详细
- <provider
- android:name="androidx.core.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>
- //file_paths内容
- <paths>
- <files-path
- name="moon"
- path="myDir/" />
- paths>
-
- File dir = new File(context.getFileDir(),"myDir");
- FIle file = new File(dir,"my.png");
- Uri contentUri = getUriForFile(getContext(), "com.mydomain.fileprovider", file);
- //得到的就是: content://com.mydomain.fileprovider/moon/my.png