实现:
1.新一个类并继承 FileProvider 类,不做其他事情
public class AppFileProvider extends FileProvider {
}
2.AndroidManifest.xml 配置:
<!-- 7.0适配临时文件申请 -->
<provider
android:name=".AppFileProvider"
android:authorities="你的包名.fileprovider"
android:exported="false"
android:grantUriPermissions="true">
<!-- 元数据 -->
<meta-data
android:name="android.support.FILE_PROVIDER_PATHS"
android:resource="@xml/file_paths" />
</provider>
3.res下新建 xml 文件夹并添加文件 file_paths.xml 内容如下:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<paths>
<external-path name="你的包名.filepath" path="." />
</paths>
</resources>
4.播放文件:
Intent intent = new Intent(Intent.ACTION_VIEW);
File file = new File(videoPathList.get(position));
Uri uri=null;
if(Build.VERSION.SDK_INT>=Build.VERSION_CODES.N){
uri = FileProvider.getUriForFile(context,"你的包名.fileprovider", file);
}else {
uri = Uri.fromFile(file);
}
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK|Intent.FLAG_ACTIVITY_CLEAR_TASK);
intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
intent.setDataAndType(uri, "video/*");
context.startActivity(intent);