第一步 最重要了, build.gradle 里 修改
targetSdk 29 不改不行啊
<!-- 网络权限 -->
<uses-permission android:name="android.permission.INTERNET" /> <!-- 写入外部存储权限 -->
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /> <!-- 读取外部存储权限 -->
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<provider
android:name="androidx.core.content.FileProvider"
android:authorities="com.baiwen.www.fileprovider"
android:exported="false"
android:grantUriPermissions="true">
<meta-data
android:name="android.support.FILE_PROVIDER_PATHS"
android:resource="@xml/file_paths" />
</provider>
</application>
<?xml version="1.0" encoding="utf-8"?>
<resources>
<paths>
<external-path
name="mypdf"
path="mypdf/" />
</paths>
</resources>
/***
* 动态申请 读写权限-------------------------------------
* 版权声明:本文为CSDN博主「路过独木桥!!」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。
* 原文链接:https://blog.csdn.net/wq892373445/article/details/104427185/
*/
private void requestMyPermissions() {
if (ContextCompat.checkSelfPermission(this,
Manifest.permission.WRITE_EXTERNAL_STORAGE)
!= PackageManager.PERMISSION_GRANTED) {
//没有授权,编写申请权限代码
ActivityCompat.requestPermissions(MainActivity.this, new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE}, 100);
} else {
Log.d("tag", "requestMyPermissions: 有写SD权限");
}
if (ContextCompat.checkSelfPermission(this,
Manifest.permission.READ_EXTERNAL_STORAGE)
!= PackageManager.PERMISSION_GRANTED) {
//没有授权,编写申请权限代码
ActivityCompat.requestPermissions(MainActivity.this, new String[]{Manifest.permission.READ_EXTERNAL_STORAGE}, 100);
} else {
Log.d("tag", "requestMyPermissions: 有读SD权限");
}
}