android 10 以上 提出了分區概念,在android 10中使用requestLegacyExternalStorage=true可以取消分區,但是在android11上面必須要有分區,在選擇相冊中,需要使用外部內容url(EXTERNAL_CONTENT_URI)
在android11中文件存儲的位置:
1.圖片(包括照片和屏幕截圖),存儲在 DCIM/ 和 Pictures/ 目錄中。系統將這些文件添加到 MediaStore.Images 表格中。
2.視頻,存儲在 DCIM/、Movies/ 和 Pictures/ 目錄中。系統將這些文件添加到 MediaStore.Video 表格中。
3.音頻文件,存儲在 Alarms/、Audiobooks/、Music/、Notifications/、Podcasts/ 和 Ringtones/ 目錄中,以及位於 Music/ 或 Movies/ 目錄中的音頻播放列表中。系統將這些文件添加到 MediaStore.Audio 表格中。
4.下載的文件,存儲在 Download/ 目錄中。在搭載 Android 10(API 級別 29)及更高版本的設備上,這些文件存儲在 MediaStore.Downloads 表格中。此表格在 Android 9(API 級別 28)及更低版本中不可用。
在項目中
1.最好使用targetSDK=29避免很多android10.11的適配文件
2.在application中添加android:requestLegacyExternalStorage="true"
3.獲取相冊的地方修改爲
public static final Intent choosePicture() {
if (Build.VERSION.SDK_INT >=30) {// Android 11 (API level 30)
return new Intent(Intent.ACTION_PICK, android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
} else {
Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
intent.setType("image/*");
return Intent.createChooser(intent, null);
}
}