适配Android8.0以后文件权限
程序员文章站
2022-03-26 10:09:31
适配Android8.0以后文件权限一、资源文件res里面创建xml文件,并创建provider_paths.xml文件provider_paths.xml文件代码如下:
适配Android8.0以后文件权限
一、资源文件res里面创建xml文件,并创建provider_paths.xml文件
provider_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>
二、在清单文件中加入权限以及相应配置
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.MOUNT_UNMOUNT_FILESYSTEMS"
tools:ignore="ProtectedPermissions" />
以下添加在application中
<provider
android:name="androidx.core.content.FileProvider"
android:authorities="${applicationId}.fileprovider"
android:exported="false"
android:grantUriPermissions="true">
<meta-data
android:name="android.support.FILE_PROVIDER_PATHS"
android:resource="@xml/provider_paths" />
</provider>
三、动态请求权限
import android.Manifest; import android.app.Activity; import android.content.Intent; import android.content.pm.PackageManager; import android.os.Bundle; import android.os.Handler; import android.os.Process; import java.util.ArrayList; import java.util.List; public class PermissionAcitivity extends Activity { public static int PERMISSION_REQ = 0x123456; private String[] mPermission = new String[]{ Manifest.permission.WRITE_EXTERNAL_STORAGE, Manifest.permission.READ_EXTERNAL_STORAGE }; private List<String> mRequestPermission = new ArrayList<String>(); @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.M) { for (String one : mPermission) { if (PackageManager.PERMISSION_GRANTED != this.checkPermission(one, Process.myPid(), Process.myUid())) { mRequestPermission.add(one); } } if (!mRequestPermission.isEmpty()) { this.requestPermissions(mRequestPermission.toArray(new String[mRequestPermission.size()]), PERMISSION_REQ); return; } } startActiviy(); } public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) { // 版本兼容 if (android.os.Build.VERSION.SDK_INT < android.os.Build.VERSION_CODES.M) { return; } if (requestCode == PERMISSION_REQ) { for (int i = 0; i < grantResults.length; i++) { for (String one : mPermission) { if (permissions[i].equals(one) && grantResults[i] == PackageManager.PERMISSION_GRANTED) { mRequestPermission.remove(one); } } } startActiviy(); } } public void onActivityResult(int requestCode, int resultCode, Intent data) { if (requestCode == PERMISSION_REQ) { if (resultCode == 0) { this.finish(); } } } public void startActiviy() { if (mRequestPermission.isEmpty()) { new Thread(new Runnable() { @Override public void run() { PermissionAcitivity.this.runOnUiThread(new Runnable() { @Override public void run() { Intent intent=null; intent = new Intent(PermissionAcitivity.this, MainActivity.class); startActivity(intent); finish(); } }); } }).start(); } else { new Handler().postDelayed(new Runnable() { @Override public void run() { PermissionAcitivity.this.finish(); } }, 3000); } } }
本文地址:https://blog.csdn.net/liuyi1234liuyi/article/details/110661700