Android所有版本的存储权限适配

第一步:在Manifest文件添加如下权限

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" android:maxSdkVersion="28" tools:ignore="ScopedStorage"/>

<uses-permission android:name="android.permission.MANAGE_EXTERNAL_STORAGE"/>

<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>

android:requestLegacyExternalStorage="true"

第二步:定义以下常量

private static final int REQUEST_CODE_FOR_READ_MEDIA_IMAGES_PERMISSION_FROM_ANDROID_13 = 0x00000003;

private static final int REQUEST_CODE_FOR_WRITE_EXTERNAL_STORAGE_PERMISSION_FROM_ANDROID_10_TO_ANDROID_12 = 0x00000005;

private static final int REQUEST_CODE_FOR_WRITE_EXTERNAL_STORAGE_PERMISSION_FOR_ANDROID_6_TO_ANDROID_9 = 0x00000006;

第三步:在需要写入文件的地方编写以下代码

java.io.OutputStream outputStream = null;

java.lang.String errorMessage = "";

java.lang.String fileName = "test_导出资料_" + java.lang.System.currentTimeMillis() + ".xls";

if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.M) {

android.util.Log.d("debug", "设备系统的版本号是大于等于安卓6.0以上的版本,需要检查运行时的危险权限");

if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.Q) {

android.util.Log.d("debug", "设备系统的版本号是大于等于安卓10.0以上的版本,说明是只能用媒介存储写入文件和读取文件");

if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.TIRAMISU) {

android.util.Log.d("debug", "设备系统的版本号是大于等于安卓13.0以上的版本,换成检查Media声音和Media视频和Media图片的权限");

int checkSelfPermissionResultForReadMediaImages = checkSelfPermission(android.Manifest.permission.READ_MEDIA_IMAGES);

android.util.Log.d("debug", "checkSelfPermissionResultForReadMediaImages->" + checkSelfPermissionResultForReadMediaImages);

if (checkSelfPermissionResultForReadMediaImages == android.content.pm.PackageManager.PERMISSION_GRANTED) {

android.util.Log.d("debug", "已经授予媒介图片的读取权限");

android.net.Uri uri = android.provider.MediaStore.Files.getContentUri("external");

android.content.ContentValues contentValues = new android.content.ContentValues();

contentValues.put(android.provider.MediaStore.MediaColumns.DISPLAY_NAME, fileName);

contentValues.put(android.provider.MediaStore.MediaColumns.MIME_TYPE, "application/x-xls");

android.content.ContentResolver contentResolver = getContentResolver();

android.net.Uri uriForInsertResult = contentResolver.insert(uri, contentValues);

try {

outputStream = contentResolver.openOutputStream(uriForInsertResult);

} catch (java.io.IOException ioException) {

ioException.printStackTrace();

errorMessage = ioException.getMessage();

}

} else {

errorMessage = "需要授予权限";

android.util.Log.d("debug", "没有授予媒介图片的读取权限");

android.util.Log.d("debug", "现在执行请求读取媒介图片的权限");

java.lang.String\[\] permission = new java.lang.String1;

permission0 = android.Manifest.permission.READ_MEDIA_IMAGES;

requestPermissions(permission, REQUEST_CODE_FOR_READ_MEDIA_IMAGES_PERMISSION_FROM_ANDROID_13);

}

int checkSelfPermissionResultForReadExternalStorage = checkSelfPermission(android.Manifest.permission.READ_EXTERNAL_STORAGE);

int checkSelfPermissionResultForWriteExternalStorage = checkSelfPermission(android.Manifest.permission.WRITE_EXTERNAL_STORAGE);

android.util.Log.d("debug", "检测完读取媒介图片权限之后再检测外部存储读取权限看看->" + checkSelfPermissionResultForReadExternalStorage);

android.util.Log.d("debug", "检测完读取媒介图片权限之后再检测外部存储写入权限看看->" + checkSelfPermissionResultForWriteExternalStorage);

} else {

android.util.Log.d("debug", "设备系统的版本号时介于10.0到12.0之间的,通过android.provider.MediaStore获取外部存储目录");

if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.R) {

android.util.Log.d("debug", "安卓11或者安卓12通过Environment的isExternalStorageManager方法检查是否有权限");

if (android.os.Environment.isExternalStorageManager()) {

android.util.Log.d("debug", "在安卓11或者安卓12中有读写文件的权限");

android.net.Uri uri = android.provider.MediaStore.Files.getContentUri("external");

android.content.ContentValues contentValues = new android.content.ContentValues();

contentValues.put(android.provider.MediaStore.MediaColumns.DISPLAY_NAME, fileName);

contentValues.put(android.provider.MediaStore.MediaColumns.MIME_TYPE, "application/x-xls");

android.content.ContentResolver contentResolver = getContentResolver();

android.net.Uri uriForInsertResult = contentResolver.insert(uri, contentValues);

try {

outputStream = contentResolver.openOutputStream(uriForInsertResult);

} catch (java.io.IOException ioException) {

ioException.printStackTrace();

errorMessage = ioException.getMessage();

}

} else {

errorMessage = "需要授予权限";

android.util.Log.d("debug", "安卓11或者安卓12的逻辑请求权限");

try {

Intent intent = new Intent(android.provider.Settings.ACTION_MANAGE_APP_ALL_FILES_ACCESS_PERMISSION);

intent.addCategory("android.intent.category.DEFAULT");

intent.setData(android.net.Uri.parse(String.format("package:%s",getApplicationContext().getPackageName())));

startActivityForResult(intent, REQUEST_CODE_FOR_WRITE_EXTERNAL_STORAGE_PERMISSION_FROM_ANDROID_10_TO_ANDROID_12);

} catch (Exception e) {

e.printStackTrace();

Intent intent = new Intent();

intent.setAction(android.provider.Settings.ACTION_MANAGE_ALL_FILES_ACCESS_PERMISSION);

startActivityForResult(intent, REQUEST_CODE_FOR_WRITE_EXTERNAL_STORAGE_PERMISSION_FROM_ANDROID_10_TO_ANDROID_12);

}

}

} else {

android.util.Log.d("debug", "在安卓10版本中沿用之前的检查方法");

int checkSelfPermissionResultForReadExternalStorage = androidx.core.app.ActivityCompat.checkSelfPermission(this, android.Manifest.permission.READ_EXTERNAL_STORAGE);

//int checkSelfPermissionResultForWriteExternalStorage = androidx.core.app.ActivityCompat.checkSelfPermission(this, android.Manifest.permission.WRITE_EXTERNAL_STORAGE);

android.util.Log.d("debug", "checkSelfPermissionResultForReadExternalStorage->" + checkSelfPermissionResultForReadExternalStorage);

//android.util.Log.d("debug", "checkSelfPermissionResultForWriteExternalStorage->" + checkSelfPermissionResultForWriteExternalStorage);

//boolean isHavePermission = checkSelfPermissionResultForReadExternalStorage == android.content.pm.PackageManager.PERMISSION_GRANTED && checkSelfPermissionResultForWriteExternalStorage == android.content.pm.PackageManager.PERMISSION_GRANTED;

if (checkSelfPermissionResultForReadExternalStorage == android.content.pm.PackageManager.PERMISSION_GRANTED) {

android.util.Log.d("debug", "已经授予外部存储写入权限");

android.net.Uri uri = android.provider.MediaStore.Files.getContentUri("external");

android.content.ContentValues contentValues = new android.content.ContentValues();

contentValues.put(android.provider.MediaStore.MediaColumns.DISPLAY_NAME, fileName);

contentValues.put(android.provider.MediaStore.MediaColumns.MIME_TYPE, "application/x-xls");

android.content.ContentResolver contentResolver = getContentResolver();

android.net.Uri uriForInsertResult = contentResolver.insert(uri, contentValues);

try {

outputStream = contentResolver.openOutputStream(uriForInsertResult);

} catch (java.io.IOException ioException) {

ioException.printStackTrace();

errorMessage = ioException.getMessage();

}

} else {

errorMessage = "需要授予权限";

android.util.Log.d("debug", "没有授予外部存储写入权限");

android.util.Log.d("debug", "现在执行请求外部存储写入的权限");

android.util.Log.d("debug", "安卓10的逻辑请求权限");

java.lang.String\[\] permission = new java.lang.String2;

permission0 = android.Manifest.permission.READ_EXTERNAL_STORAGE;

permission1 = android.Manifest.permission.WRITE_EXTERNAL_STORAGE;

requestPermissions(permission, REQUEST_CODE_FOR_WRITE_EXTERNAL_STORAGE_PERMISSION_FROM_ANDROID_10_TO_ANDROID_12);

}

}

}

} else {

android.util.Log.d("debug", "设备系统的版本号是介于6.0到9.0之间,仍然是可以从Environment类的getExternalStorageDirectory方法获取外部存储目录的,但是前提是要检查运行时权限");

int checkSelfPermissionResultForWriteExternalStorage = checkSelfPermission(android.Manifest.permission.WRITE_EXTERNAL_STORAGE);

if (checkSelfPermissionResultForWriteExternalStorage == android.content.pm.PackageManager.PERMISSION_GRANTED) {

java.io.File externalStorageDirectory = android.os.Environment.getExternalStorageDirectory();

android.util.Log.d("debug", "安卓系统是在6.0到9.0之间的,通过Environment类获取到外部存储目录->" + externalStorageDirectory.getPath());

try {

outputStream = new java.io.FileOutputStream(externalStorageDirectory.getPath() + java.io.File.separator + fileName);

} catch (java.io.IOException ioException) {

ioException.printStackTrace();

errorMessage = ioException.getMessage();

}

} else {

errorMessage = "需要授予权限";

java.lang.String\[\] permission = new java.lang.String1;

permission0 = android.Manifest.permission.WRITE_EXTERNAL_STORAGE;

requestPermissions(permission, REQUEST_CODE_FOR_WRITE_EXTERNAL_STORAGE_PERMISSION_FOR_ANDROID_6_TO_ANDROID_9);

}

}

} else {

android.util.Log.d("debug", "设备系统的版本号小于6.0以下的版本,不需要检查运行时权限");

java.io.File externalStorageDirectory = android.os.Environment.getExternalStorageDirectory();

android.util.Log.d("debug", "直接通过Environment类的getExternalStorageDirectory()即可获取外部存储目录->" + externalStorageDirectory.getPath());

try {

outputStream = new java.io.FileOutputStream(externalStorageDirectory.getPath() + java.io.File.separator + fileName);

} catch (java.io.IOException ioException) {

ioException.printStackTrace();

errorMessage = ioException.getMessage();

}

}

if (outputStream != null) {

//file write

} else {

android.widget.Toast.makeText(this, errorMessage, android.widget.Toast.LENGTH_LONG).show();

}

第四步:权限请求回调函数

@Override

public void onRequestPermissionsResult(int requestCode, String\[\] permissions, int\[\] grantResults) {

super.onRequestPermissionsResult(requestCode, permissions, grantResults);

if (requestCode == REQUEST_CODE_FOR_READ_MEDIA_IMAGES_PERMISSION_FROM_ANDROID_13) {

if (grantResults.length > 0 && grantResults0 == android.content.pm.PackageManager.PERMISSION_GRANTED) {

if (permissions.length > 0) {

android.util.Log.d("debug", "打印授予的权限->" + permissions0);

}

java.lang.String fileName = "buyup_导出资料_" + java.lang.System.currentTimeMillis() + ".xls";

java.lang.String errorMessage = "";

java.io.OutputStream outputStream = null;

android.util.Log.d("debug", "设备系统的版本号是大于等于安卓10.0以上的版本,说明是只能用媒介存储写入文件和读取文件");

android.net.Uri uri = android.provider.MediaStore.Files.getContentUri("external");

android.content.ContentValues contentValues = new android.content.ContentValues();

contentValues.put(android.provider.MediaStore.MediaColumns.DISPLAY_NAME, fileName);

contentValues.put(android.provider.MediaStore.MediaColumns.MIME_TYPE, "application/x-xls");

android.content.ContentResolver contentResolver = getContentResolver();

android.net.Uri uriForInsertResult = contentResolver.insert(uri, contentValues);

try {

outputStream = contentResolver.openOutputStream(uriForInsertResult);

} catch (java.io.IOException ioException) {

ioException.printStackTrace();

errorMessage = ioException.getMessage();

}

if (outputStream != null) {

//file write

} else {

android.widget.Toast.makeText(this, errorMessage, android.widget.Toast.LENGTH_LONG).show();

}

} else {

if (permissions.length > 0) {

java.lang.String permission0 = permissions0;

android.util.Log.d("debug", "打印请求的权限->" + permission0);

}

android.util.Log.d("debug", "权限授予失败");

android.widget.Toast.makeText(this, "权限授予失败", android.widget.Toast.LENGTH_LONG).show();

}

} else if (requestCode == REQUEST_CODE_FOR_WRITE_EXTERNAL_STORAGE_PERMISSION_FOR_ANDROID_6_TO_ANDROID_9) {

if (grantResults.length > 0 && grantResults0 == android.content.pm.PackageManager.PERMISSION_GRANTED) {

if (permissions.length > 0) {

android.util.Log.d("debug", "来自安卓6.0到安卓9.9之间的请求权限,打印授予的权限->" + permissions0);

}

java.lang.String fileName = "buyup_导出资料_" + java.lang.System.currentTimeMillis() + ".xls";

java.lang.String errorMessage = "";

java.io.OutputStream outputStream = null;

android.util.Log.d("debug", "设备系统的版本号是大于等于安卓6.0到安卓9.0之间的,通过用android.os.Environment类获取外部存储");

java.io.File externalStorageDirectory = android.os.Environment.getExternalStorageDirectory();

try {

outputStream = new java.io.FileOutputStream(externalStorageDirectory.getPath() + java.io.File.separator + fileName);

} catch (java.io.IOException ioException) {

ioException.printStackTrace();

errorMessage = ioException.getMessage();

}

if (outputStream != null) {

//file write

} else {

android.widget.Toast.makeText(this, errorMessage, android.widget.Toast.LENGTH_LONG).show();

}

} else {

if (permissions.length > 0) {

android.util.Log.d("debug", "来自安卓6.0到安卓9.9之间的请求权限,打印拒绝的权限->" + permissions0);

}

android.util.Log.d("debug", "来自安卓6.0到安卓9.9之间的请求权限,权限授予失败");

android.widget.Toast.makeText(this, "权限授予失败", android.widget.Toast.LENGTH_LONG).show();

}

} else if (requestCode == REQUEST_CODE_FOR_WRITE_EXTERNAL_STORAGE_PERMISSION_FROM_ANDROID_10_TO_ANDROID_12) {

if (grantResults.length > 0 && grantResults0 == android.content.pm.PackageManager.PERMISSION_GRANTED) {

if (permissions.length > 0) {

android.util.Log.d("debug", "来自安卓10.0的版本,打印授予的权限->" + permissions0);

android.util.Log.d("debug", "来自安卓10.0的版本,打印授予的权限->" + permissions1);

}

java.lang.String fileName = "buyup_导出资料_" + java.lang.System.currentTimeMillis() + ".xls";

java.lang.String errorMessage = "";

java.io.OutputStream outputStream = null;

android.net.Uri uri = android.provider.MediaStore.Files.getContentUri("external");

android.content.ContentValues contentValues = new android.content.ContentValues();

contentValues.put(android.provider.MediaStore.MediaColumns.DISPLAY_NAME, fileName);

contentValues.put(android.provider.MediaStore.MediaColumns.MIME_TYPE, "application/x-xls");

android.content.ContentResolver contentResolver = getContentResolver();

android.net.Uri uriForInsertResult = contentResolver.insert(uri, contentValues);

try {

outputStream = contentResolver.openOutputStream(uriForInsertResult);

} catch (java.io.IOException ioException) {

ioException.printStackTrace();

errorMessage = ioException.getMessage();

}

if (outputStream != null) {

//file write

} else {

android.widget.Toast.makeText(this, errorMessage, android.widget.Toast.LENGTH_LONG).show();

}

} else {

if (permissions.length > 0) {

android.util.Log.d("debug", "来自安卓10.0的版本,打印拒绝的权限->" + permissions0);

android.util.Log.d("debug", "来自安卓10.0的版本,打印拒绝的权限->" + permissions1);

}

android.widget.Toast.makeText(this, "权限授予失败", android.widget.Toast.LENGTH_LONG).show();

}

}

}

第五步:针对安卓11或者安卓12版本的请求权限方法

@Override

protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {

super.onActivityResult(requestCode, resultCode, data);

if (requestCode == REQUEST_CODE_FOR_WRITE_EXTERNAL_STORAGE_PERMISSION_FROM_ANDROID_10_TO_ANDROID_12) {

android.util.Log.d("debug", "在安卓11或者安卓12系统中引导到系统设置界面开启权限回来");

if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.R) {

if (android.os.Environment.isExternalStorageManager()) {

android.util.Log.d("debug", "已经授予外部存储写入权限");

java.io.OutputStream outputStream = null;

java.lang.String errorMessage = "";

java.lang.String fileName = "buyup_导出资料_" + java.lang.System.currentTimeMillis() + ".xls";

android.util.Log.d("debug", "设备系统的版本号时介于10.0到12.0之间的,通过android.provider.MediaStore获取外部存储目录");

android.net.Uri uri = android.provider.MediaStore.Files.getContentUri("external");

android.content.ContentValues contentValues = new android.content.ContentValues();

contentValues.put(android.provider.MediaStore.MediaColumns.DISPLAY_NAME, fileName);

contentValues.put(android.provider.MediaStore.MediaColumns.MIME_TYPE, "application/x-xls");

android.content.ContentResolver contentResolver = getContentResolver();

android.net.Uri uriForInsertResult = contentResolver.insert(uri, contentValues);

try {

outputStream = contentResolver.openOutputStream(uriForInsertResult);

} catch (java.io.IOException ioException) {

ioException.printStackTrace();

errorMessage = ioException.getMessage();

}

if (outputStream != null) {

//file write

} else {

android.widget.Toast.makeText(this, errorMessage, android.widget.Toast.LENGTH_LONG).show();

}

} else {

android.widget.Toast.makeText(this, "需要授予权限才能导出文件!", android.widget.Toast.LENGTH_SHORT).show();

}

}

}

}

相关推荐
论文复现现场16 分钟前
RTX 4090 24GB 能跑 Qwen3.8-27B 吗?单卡显存计算与云端部署指南
人工智能·python·云计算·llama·gpu算力
Patrick在香港22 分钟前
模型路由器实测:12 个任务省 77%,旗舰过载时的降级要打出显式标记
python·llm·智能路由器·api·架构设计·成本优化
Looooking31 分钟前
Python 之 jwt 令牌生成和校验
python·jwt
渡我白衣1 小时前
并查集:基础认识与模拟实现
android·java·javascript·数据结构·c++·算法·并查集
天下无敌笨笨熊1 小时前
C#Modbus串口开发心得
开发语言·c#
电商API_180079052471 小时前
电商商品价格监控工具淘宝京东商品价格抓取API项目实操分享
java·大数据·开发语言·前端·数据挖掘
zzzll11111 小时前
深度剖析:HashMap 并发死循环与 ConcurrentHashMap 两代演进全解
java·开发语言
如意猴1 小时前
【C++】001--C++入门(1)
开发语言·c++
传奇开心果编程1 小时前
【Rust入门知识点学与练】第10课:Option 和 Result(错误处理入门)
开发语言·学习·rust
2601_962295992 小时前
嵌入式开发语言用哪个最好,C,python或者其他
c语言·python·嵌入式开发·开发效率·系统资源