Android--存储权限
Posted chaoyu168
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Android--存储权限相关的知识,希望对你有一定的参考价值。
android11 更新了新的存储权限分区存储,将不再允许用户授权访问 Downloads 的根目录、每个可用 SD 卡的根目录以及其它应用的目录。应用仍然可以通过 Storage Access Framework API 或者文件选择器来帮助用户从共享存储中选取个别文件。
Android SDK大于等于30(Android11)在 AndroidManifest.xml中申请,并且代码中也需要动态申请:
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<!-- Android11新增 -->
<uses-permission android:name="android.permission.MANAGE_EXTERNAL_STORAGE" />
<application
android:name=".application.MainApplication"
android:allowBackup="true"
android:icon="@mipmap/biao"
android:label="@string/app_name"
android:persistent="true"
android:requestLegacyExternalStorage="true"
android:sharedUserId="android.uid.system"
android:supportsRtl="true"
android:theme="@style/AppTheme"
android:preserveLegacyExternalStorage="true"
android:usesCleartextTraffic="true">
</application>
<!-- android10.0根目录创建文件夹android:requestLegacyExternalStorage="true" -->
<!-- android11 根目录创建文件夹android:preserveLegacyExternalStorage="true"
在Android11的机器上覆盖安装时,才能访问旧版存储位置,卸载重装会失效-->
public final class PermissionActivity extends AppCompatActivity
private static final int REQUEST_CODE = 1024;
@Override
protected void onCreate(@Nullable Bundle savedInstanceState)
super.onCreate(savedInstanceState);
requestPermission();
private void requestPermission()
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R)
// 先判断有没有权限
if (Environment.isExternalStorageManager())
doSomething();
else
Intent intent = new Intent(Settings.ACTION_MANAGE_APP_ALL_FILES_ACCESS_PERMISSION);
intent.setData(Uri.parse("package:" + context.getPackageName()));
startActivityForResult(intent, REQUEST_CODE);
else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M)
// 先判断有没有权限
if (ActivityCompat.checkSelfPermission(this, Manifest.permission.READ_EXTERNAL_STORAGE) == PackageManager.PERMISSION_GRANTED &&
ContextCompat.checkSelfPermission(this, Manifest.permission.WRITE_EXTERNAL_STORAGE) == PackageManager.PERMISSION_GRANTED)
doSomething();
else
ActivityCompat.requestPermissions(this, new String[]Manifest.permission.READ_EXTERNAL_STORAGE, Manifest.permission.WRITE_EXTERNAL_STORAGE, REQUEST_CODE);
else
doSomething();
@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults)
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
if (requestCode == REQUEST_CODE)
if (ActivityCompat.checkSelfPermission(this, Manifest.permission.READ_EXTERNAL_STORAGE) == PackageManager.PERMISSION_GRANTED &&
ContextCompat.checkSelfPermission(this, Manifest.permission.WRITE_EXTERNAL_STORAGE) == PackageManager.PERMISSION_GRANTED)
doSomething();
else
ToastUtils.show("存储权限获取失败");
@Override
protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data)
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == REQUEST_CODE && Build.VERSION.SDK_INT >= Build.VERSION_CODES.R)
if (Environment.isExternalStorageManager())
writeFile();
else
ToastUtils.show("存储权限获取失败");
/**
* 模拟文件写入
*/
private void doSomething()
ToastUtils.show("doSomething");
以上是关于Android--存储权限的主要内容,如果未能解决你的问题,请参考以下文章
Android——Android10的分区存储(Scoped Storage)
Android Q (10) 请求访问所有存储的权限。范围存储