Storage updates in Android 11

文章推薦指數: 80 %
投票人數:10人

Migrate data to directories that are visible when using scoped storage ... system further restricts your app's access to other apps' private directories. Platform AndroidStudio GooglePlay Jetpack Kotlin Docs Games Language English BahasaIndonesia Español–AméricaLatina Português–Brasil 中文–简体 日本語 한국어 Signin Platform Overview Releases Devices Wear Technology LargeScreens MachineLearning Privacy Android5G Platform Overview Releases Devices Wear Technology LargeScreens MachineLearning Privacy Android5G AndroidStudio GooglePlay Jetpack Kotlin Docs Games AllAndroidreleases Android11 Home Privacyupdates Overview Storage Permissions Location Packagevisibility Dataaccessauditing MACaddresses Foregroundservices FeaturesandAPIs Overview Add5Gcapabilitiestoyourapp CaptiveportalAPIsupport Securesharingoflargedatasets Peopleandconversations AndroidintheEnterprise Toasts Controlexternaldevices Integrateautofillwithkeyboards Behaviorchangesforapps Allapps AppstargetingAndroid11 Non-SDKrestrictions GetstartedwithAndroid11 Migrateyourapps GetAndroid11 SetuptheSDK Referenceupdates APIDiffReport(API30) BroadcastIntents(API30) CommonIntents(API30) Compatibilityframeworkchanges(Android11) More Dashboards AndroidDevelopers Platform Releases StorageupdatesinAndroid 11 Android 11(APIlevel 30)furtherenhancestheplatform,givingbetter protectiontoappanduserdataonexternalstorage.Thisreleaseintroduces severalenhancements,suchasrawfilepathaccess,batchedit operationsformedia,andanupdatedUIfortheStorageAccessFramework. Thereleasealsooffersimprovementstoscoped storage,whichmakesiteasierfor developerstofulfilltheirstorageuse casesaftertheymigratetousingthis storagemodel. Scopedstorageenforcement AppsthatrunonAndroid 11buttargetAndroid10 (APIlevel29)canstillrequestthe requestLegacyExternalStorage attribute.Thisflagallowsappstotemporarilyoptoutofthe changesassociated withscopedstorage,suchasgrantingaccesstodifferentdirectoriesand differenttypesofmediafiles.Afteryouupdateyourapptotarget Android 11,thesystemignorestherequestLegacyExternalStorage flag. MaintaincompatibilitywithAndroid10 IfyourappoptsoutofscopedstoragewhenrunningonAndroid10devices,it's recommendedthatyoucontinuetosetrequestLegacyExternalStoragetotruein yourapp'smanifestfile.Thatway,yourappcancontinuetobehaveasexpected ondevicesthatrunAndroid10. Migratedatatodirectoriesthatarevisiblewhenusingscopedstorage IfyourappusesthelegacystoragemodelandpreviouslytargetedAndroid10or lower,youmightbestoringdatainadirectorythatyourappcannotaccesswhen thescopedstoragemodelisenabled. BeforeyoutargetAndroid 11,migrate datatoadirectory that'scompatiblewithscopedstorage. Testscopedstorage Toenablescopedstorageinyourapp,regardlessofyourapp'stargetSDK versionandmanifestflagvalues,enablethefollowingappcompatibilityflags: DEFAULT_SCOPED_STORAGE (enabledforallappsbydefault) FORCE_ENABLE_SCOPED_STORAGE (disabledforallappsbydefault) Todisablescopedstorageandusethelegacystoragemodelinstead, unsetbothflags. Managedevicestorage StartinginAndroid 11,appsthatusethescopedstoragemodel canaccessonlytheirownapp-specificcachefiles.Ifyourappneedstomanage devicestorage,followtheinstructionsonhowtoqueryfree space. Checkforfreespacebyinvokingthe ACTION_MANAGE_STORAGEintentaction. Ifthereisn'tenoughfreespaceonthedevice,prompttheusertogiveyour appconsenttoclearallcaches.Todoso,invokethe ACTION_CLEAR_APP_CACHEintentaction. Caution:TheACTION_CLEAR_APP_CACHEintentactioncansubstantiallyaffect devicebatterylifeandmightremovealargenumberoffilesfromthe device. App-specificdirectoryonexternalstorage StartinginAndroid 11,appscannotcreatetheirown app-specificdirectoryonexternalstorage.To accessthedirectorythatthesystemprovidesforyourapp,call getExternalFilesDirs(). Mediafileaccess Tomakeiteasiertoaccessmediawhileretaininguserprivacy, Android 11addsthefollowingcapabilities. Performbatchoperations Forconsistencyacrossdevicesandaddeduserconvenience, Android 11addsseveralmethodsthatmakeiteasiertomanage groupsofmediafiles. Accessfilesusingdirectfilepathsandnativelibraries Tohelpyourappworkmoresmoothlywiththird-partymedialibraries, Android 11allowsyoutouseAPIsotherthanthe MediaStoreAPItoaccess mediafilesfromsharedstorageusingdirectfile paths.TheseAPIs includethefollowing: TheFileAPI. Nativelibraries,suchasfopen(). Accesstodatafromotherapps Toprotectuserprivacy,ondevicesthatrunAndroid 11or higher,thesystemfurtherrestrictsyourapp'saccesstootherapps'private directories. Accesstodatadirectoriesoninternalstorage Android9(APIlevel28)startedtorestrictwhichappscouldmakethefilesin theirdatadirectoriesoninternal storageworld-accessibletoother apps.AppsthattargetAndroid9orhighercannotmakethefilesintheirdata directories world-accessible. Android 11expandsuponthisrestriction.Ifyourapptargets Android 11,itcannotaccessthefilesinanyotherapp'sdata directory,eveniftheotherapptargetsAndroid8.1(APIlevel27)orlowerand hasmadethefilesinitsdatadirectoryworld-readable. Accesstoapp-specificdirectoriesonexternalstorage OnAndroid 11,appscannolongeraccessfilesinanyother app'sdedicated,app-specific directorywithinexternal storage. Documentaccessrestrictions Togivedeveloperstimefortesting,thefollowingchangesrelatedtothe StorageAccessFramework(SAF)takeeffectonlyifyourapptargets Android 11orhigher. Accesstodirectories Youcannolongerusethe ACTION_OPEN_DOCUMENT_TREE intentactiontorequestaccesstothefollowingdirectories: Therootdirectoryoftheinternalstoragevolume. TherootdirectoryofeachSDcardvolumethatthedevicemanufacturer considerstobereliable,regardlessofwhetherthecardisemulatedor removable.Areliablevolumeisonethatanappcansuccessfullyaccessmostof thetime. TheDownloaddirectory. Accesstofiles Youcannolongerusethe ACTION_OPEN_DOCUMENT_TREE orthe ACTION_OPEN_DOCUMENT intentactiontorequestthattheuserselectindividualfilesfromthe followingdirectories: TheAndroid/data/directoryandallsubdirectories. TheAndroid/obb/directoryandallsubdirectories. Testthechange Totestthisbehaviorchange,dothefollowing: InvokeanintentwiththeACTION_OPEN_DOCUMENTaction.Checkthatthe Android/data/andAndroid/obb/directoriesbothdon'tappear. Dooneofthefollowing: Enablethe RESTRICT_STORAGE_ACCESS_FRAMEWORK appcompatibilityflag. TargetAndroid 11orhigher. InvokeanintentwiththeACTION_OPEN_DOCUMENT_TREEaction.Checkthatthe Downloaddirectoryappearsandtheactionbuttonassociatedwiththedirectory isgrayedout. Permissions Android 11introducesthefollowingchangesrelatedtostorage permissions. Targetanyversion Figure1.Dialogshownwhenanappusesscopedstorageand requeststheREAD_EXTERNAL_STORAGEpermission. ThefollowingchangestakeeffectinAndroid 11,regardlessofyourapp'stargetSDKversion: TheStorageruntimepermissionisrenamedtoFiles& Media. Ifyourapphasn'toptedoutof scopedstorageand requeststhe READ_EXTERNAL_STORAGE permission,usersseeadifferentdialogcomparedtoAndroid10.Thedialog indicatesthatyourappisrequestingaccesstophotosandmedia,asshownin Figure1. UserscanseewhichappshavetheREAD_EXTERNAL_STORAGE permissioninsystemsettings.OntheSettings>Privacy> Permissionmanager>Filesandmediapage,eachappthathasthe permissionislistedunderAllowedforallfiles.Ifyourapptargets Android 11,keepinmindthatthisaccessto"allfiles"is read-only.Toreadandwritetoallfilesin sharedstorageusingthisapp,you needtohavetheallfiles accesspermission. TargetAndroid 11 IfyourapptargetsAndroid 11,boththe WRITE_EXTERNAL_STORAGEpermissionandtheWRITE_MEDIA_STORAGEprivileged permissionnolongerprovideanyadditionalaccess. Keepinmindthat,ondevicesthatrunAndroid10(APIlevel29)orhigher,your appcancontributetowell-definedmediacollectionssuchas MediaStore.Downloadswithoutrequestinganystorage-relatedpermissions.Learn moreabouthowtorequestonlythenecessary permissionswhen workingwithmediafilesinyourapp. Allfilesaccess Themajorityofappsthatrequiresharedstorageaccesscanfollowthebest practicesforsharingmedia filesandsharingnon-media files.However,some appshaveacoreusecasethatrequiresbroadaccessoffilesonadevice,but cannotdosoefficientlyusingtheprivacy-friendlystoragebestpractices. AndroidprovidesaspecialappaccesscalledAllfilesaccessforthese situations.Tolearnmore,seetheguideonhowtomanageall filesonastoragedevice. Note:IfyoupublishyourapptoGooglePlay,carefullyreadthe notice. IfyoutargetAndroid 11anddeclareAllfilesaccess,itcan affectyourabilitytopublishandupdateyourapponGooglePlay. Additionalresources FormoreinformationaboutchangestostorageinAndroid11,viewthefollowing materials: Blogposts Android11Storage FAQ Videos Howtoperformstorageaccessin Android11 ContentandcodesamplesonthispagearesubjecttothelicensesdescribedintheContentLicense.JavaisaregisteredtrademarkofOracleand/oritsaffiliates. Lastupdated2021-11-11UTC. [{ "type":"thumb-down", "id":"missingTheInformationINeed", "label":"MissingtheinformationIneed" },{ "type":"thumb-down", "id":"tooComplicatedTooManySteps", "label":"Toocomplicated/toomanysteps" },{ "type":"thumb-down", "id":"outOfDate", "label":"Outofdate" },{ "type":"thumb-down", "id":"samplesCodeIssue", "label":"Samples/codeissue" },{ "type":"thumb-down", "id":"otherDown", "label":"Other" }] [{ "type":"thumb-up", "id":"easyToUnderstand", "label":"Easytounderstand" },{ "type":"thumb-up", "id":"solvedMyProblem", "label":"Solvedmyproblem" },{ "type":"thumb-up", "id":"otherUp", "label":"Other" }] Twitter Follow@AndroidDevonTwitter YouTube CheckoutAndroidDevelopersonYouTube MoreAndroid Android AndroidforEnterprise Security Source News Blog Podcasts Discover Gaming MachineLearning Privacy 5G AndroidDevices Largescreens WearOS AndroidTV Androidforcars AndroidThings ChromeOSdevices Releases Android11 Android10 Pie Oreo Nougat Marshmallow Lollipop KitKat DocumentationandDownloads AndroidStudioguide Developersguides APIreference DownloadStudio AndroidNDK Support Reportplatformbug Reportdocumentationbug GooglePlaysupport Joinresearchstudies Android Chrome Firebase GoogleCloudPlatform Allproducts Privacy License Brandguidelines Getnewsandtipsbyemail Subscribe Language English BahasaIndonesia Español–AméricaLatina Português–Brasil 中文–简体 日本語 한국어



請為這篇文章評分?