? ? ? ? ? ? ?第一步:在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", "設備系統(tǒng)的版本號是大于等于安卓6.0以上的版本,需要檢查運行時的危險權限");
? ? ? ? ? ? ? ? if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.Q) {
? ? ? ? ? ? ? ? ? ? android.util.Log.d("debug", "設備系統(tǒng)的版本號是大于等于安卓10.0以上的版本,說明是只能用媒介存儲寫入文件和讀取文件");
? ? ? ? ? ? ? ? ? ? if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.TIRAMISU) {
? ? ? ? ? ? ? ? ? ? ? ? android.util.Log.d("debug", "設備系統(tǒng)的版本號是大于等于安卓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", "已經(jīng)授予媒介圖片的讀取權限");
? ? ? ? ? ? ? ? ? ? ? ? ? ? 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", "現(xiàn)在執(zhí)行請求讀取媒介圖片的權限");
? ? ? ? ? ? ? ? ? ? ? ? ? ? java.lang.String[] permission = new java.lang.String[1];
? ? ? ? ? ? ? ? ? ? ? ? ? ? permission[0] = 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", "設備系統(tǒng)的版本號時介于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", "已經(jīng)授予外部存儲寫入權限");
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? 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", "現(xiàn)在執(zhí)行請求外部存儲寫入的權限");
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? android.util.Log.d("debug", "安卓10的邏輯請求權限");
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? java.lang.String[] permission = new java.lang.String[2];
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? permission[0] = android.Manifest.permission.READ_EXTERNAL_STORAGE;
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? permission[1] = 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", "設備系統(tǒng)的版本號是介于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", "安卓系統(tǒng)是在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.String[1];
? ? ? ? ? ? ? ? ? ? ? ? permission[0] = 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", "設備系統(tǒng)的版本號小于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();
? ? ? ? ? ? }
?? ? ? ?第四步:權限請求回調(diào)函數(shù)
?? ? ? ?@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 && grantResults[0] == android.content.pm.PackageManager.PERMISSION_GRANTED) {
? ? ? ? ? ? ? ? if (permissions.length > 0) {
? ? ? ? ? ? ? ? ? ? android.util.Log.d("debug", "打印授予的權限->" + permissions[0]);
? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? java.lang.String fileName = "buyup_導出資料_" + java.lang.System.currentTimeMillis() + ".xls";
? ? ? ? ? ? ? ? java.lang.String errorMessage = "";
? ? ? ? ? ? ? ? java.io.OutputStream outputStream = null;
? ? ? ? ? ? ? ? android.util.Log.d("debug", "設備系統(tǒng)的版本號是大于等于安卓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 = permissions[0];
? ? ? ? ? ? ? ? ? ? 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 && grantResults[0] == android.content.pm.PackageManager.PERMISSION_GRANTED) {
? ? ? ? ? ? ? ? if (permissions.length > 0) {
? ? ? ? ? ? ? ? ? ? android.util.Log.d("debug", "來自安卓6.0到安卓9.9之間的請求權限,打印授予的權限->" + permissions[0]);
? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? java.lang.String fileName = "buyup_導出資料_" + java.lang.System.currentTimeMillis() + ".xls";
? ? ? ? ? ? ? ? java.lang.String errorMessage = "";
? ? ? ? ? ? ? ? java.io.OutputStream outputStream = null;
? ? ? ? ? ? ? ? android.util.Log.d("debug", "設備系統(tǒng)的版本號是大于等于安卓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之間的請求權限,打印拒絕的權限->" + permissions[0]);
? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? 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 && grantResults[0] == android.content.pm.PackageManager.PERMISSION_GRANTED) {
? ? ? ? ? ? ? ? if (permissions.length > 0) {
? ? ? ? ? ? ? ? ? ? android.util.Log.d("debug", "來自安卓10.0的版本,打印授予的權限->" + permissions[0]);
? ? ? ? ? ? ? ? ? ? android.util.Log.d("debug", "來自安卓10.0的版本,打印授予的權限->" + permissions[1]);
? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? 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的版本,打印拒絕的權限->" + permissions[0]);
? ? ? ? ? ? ? ? ? ? android.util.Log.d("debug", "來自安卓10.0的版本,打印拒絕的權限->" + permissions[1]);
? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? android.widget.Toast.makeText(this, "權限授予失敗", android.widget.Toast.LENGTH_LONG).show();
? ? ? ? ? ? }
? ? ? ? }
? ? }文章來源:http://www.zghlxwxcb.cn/news/detail-796324.html
? ? 第五步:針對安卓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系統(tǒng)中引導到系統(tǒng)設置界面開啟權限回來");
? ? ? ? ? ? if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.R) {
? ? ? ? ? ? ? ? if (android.os.Environment.isExternalStorageManager()) {
? ? ? ? ? ? ? ? ? ? android.util.Log.d("debug", "已經(jīng)授予外部存儲寫入權限");
? ? ? ? ? ? ? ? ? ? java.io.OutputStream outputStream = null;
? ? ? ? ? ? ? ? ? ? java.lang.String errorMessage = "";
? ? ? ? ? ? ? ? ? ? java.lang.String fileName = "buyup_導出資料_" + java.lang.System.currentTimeMillis() + ".xls";
? ? ? ? ? ? ? ? ? ? android.util.Log.d("debug", "設備系統(tǒng)的版本號時介于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();
? ? ? ? ? ? ? ? }
? ? ? ? ? ? }
? ? ? ? }
? ? }文章來源地址http://www.zghlxwxcb.cn/news/detail-796324.html
到了這里,關于Android所有版本的存儲權限適配的文章就介紹完了。如果您還想了解更多內(nèi)容,請在右上角搜索TOY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關文章,希望大家以后多多支持TOY模板網(wǎng)!