1.新一个类并继承 FileProvider 类,不做其他事情
public class AppFileProvider extends FileProvider {
}
2.AndroidManifest.xml 配置:
<provider
android:name=".provider.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"?>
<paths xmlns:tools="http://schemas.android.com/tools"
xmlns:android="http://schemas.android.com/apk/res/android"
tools:ignore="MissingDefaultResource">
<files-path
name="int_root"
path="/" />
<external-path
name="ext_root"
path="/"/>
<external-files-path
name="ext_file"
path="/"/>
/>
<external-cache-path
name="ext_path"
path="/"/>
/>
</paths>
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);
可以了,亲测无问题,