Android通过路径获取URI(全版本)
1.res文件下新增xml文件夹
2.新增file_paths.xml文件
<?xml version="1.0" encoding="utf-8"?>
<paths xmlns:android="http://schemas.android.com/apk/res/android">
<external-path name="external_files" path="."/>
</paths>
3.AndroidManifest配置(在application中)
<application
android:name=".BaseApplication"
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<provider
android:name="android.support.v4.content.FileProvider"
android:authorities="com.ztgf.share_se.fileprovider"
android:exported="false"
android:grantUriPermissions="true">
<meta-data
android:name="android.support.FILE_PROVIDER_PATHS"
android:resource="@xml/file_paths" />
</provider>
</application>
4.判断系统应该使用什么方式进行调用(pakgename修改为当前包名)
public static Uri getUri(Context context,String url){
File tempFile = new File(url);
//判断版本
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) { //如果在Android7.0以上,使用FileProvider获取Uri
try{
return FileProvider.getUriForFile(context, "pakgename.fileprovider", tempFile);
}catch (Exception e){
e.printStackTrace();
}
} else { //否则使用Uri.fromFile(file)方法获取Uri
return Uri.fromFile(tempFile);
}
return null;
}
还没有评论,来说两句吧...