Storage Model | Android 101 - ProAndroidDev

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

Starting Android 10 (API 29), the model has become ... Any app that wants to read files stored on the file system will request ... SigninProAndroidDevSubmitArchiveAboutEventsdroidconStorageModel|Android101NikhilBansalFollowSep27·3minreadImageSource:android.comWiththerecentyearsofAndroidplatformupdates,thestoragemodelhasevolved.StartingAndroid10(API29),themodelhasbecomepurpose-focusedratherthanlocation-focused.We’llgettothatinabit.Butfirst,let’sunderstandthebackstory.BackStoryAnyonewhohassavedfilesonlocalstorage,beitSQLitedatabaseorinformofotherfiles,hasdeclaredREAD_EXTERNAL_STORAGEandWRITE_EXTERNAL_STORAGEpermissions.Thepremisewassimple.AnyappthatwantstoreadfilesstoredonthefilesystemwillrequestREAD_EXTERNAL_STORAGEpermissionandthesameforwritepermission.Thisgaveappsaccesstoabroaderspectrumoffilelocations.Forthemajorityoftheapps,thislevelofaccessmightnotbeneeded.Inthisnewageofprivacy,usersgetspookedeasilywhenitcomestoaskingpermissionsandstartingAndroid10(API29),thisisexactlywhattheteamsoughtouttosolve.NewAgeAndroidstoragemodelhasnowbeenrevampedtoavoidaskinguserspermissionforstorageaccessbyprovidingsomelevelofaccessbydefault.Appscannowread/writeintotheirapp-specificdirectorieswithoutaskingusers.Thisgivesappsscopedaccesstostorage.IMPORTANT!Internalstorageworksasexpected.Appscanaccesstheirapp-specificdirectorywithoutanyrestrictions.Filesarenowattributedtotheappswhichcreated/editedthem.Thismeansphotosclickedbyacameraappwillbeattributedtothesameappanditdoesn’tneedanypermissionstoaccess/editthesefiles.Asanefforttoreducediskclutter,dedicatedfiledirectoriesforimages,videos,audio,downloads,etcwillbeusedinsharedstorage.Accesstothetop-leveldirectoriesinsharedstorageisnotavailableanymore.Ifanyappusesacustomtop-leveldirectory,itishighlyrecommendedtomovetoapp-specificdirectoriesinthedefaultfolders.Environment.getExternalStorageDirectory()APIisnowdeprecated.ThepathreturnedbythiswillnotbeaccessibleandAndroidStudiowillalsowarnwhentargetingAPI29andabove.Runtimepermissionnowgivesonlyreadaccesstomedia.MediaFilesOnAndroid10,anyaccesstomediafileswillonlyworkwithMediaStoreAPIs.Anyfilepath-basedAPIslikeFilewillnotwork.However,aftertakingfeedbackfromthecommunity,thisbehaviorhasbeenreversedinAndroid11.Thelevelofaccesswillstillbescopedhoweverfilepath-basedAPIscanbeused.ToworkwithFileAPIsonAndroid10,requestLegacyExternalStoragemanifestflagwillhavetobeused.🚧NOTE:ThisflagwillnotworkonAndroid11andallappsmustmakechangesaccordingtoscopedstorage.LocationMeta-data📍Locationmeta-dataassociatedwithfilesisnowgatedbehindanewpermissionACCESS_MEDIA_LOCATIONpermission.Bydefault,thelocationparamswillnotbeincludedintheExifInterfaceAPI.LATITUDEandLONGITUDEcolumnsintheMediaStorewillalsoreturnnullonAPI29andabove.AllFilesaccessThereareappslikefilemanagersandbackupappswhicharesupposedtohavewideaccesstostorageandusersrelyonthemtoperformcertainoperations.Forsuchusecases,newpermissioni.e.MANAGE_EXTERNAL_STORAGEhasbeenintroduced.Thispermissiongiveswriteaccesstoallsharedfiles.Sincethisisspecialpermission,theappsusingthispermissionwillbesubjecttoamanualGooglePlayreview.Thereviewteamwilldecideifthispermissionisactuallyrequiredorifscopedstoragemightalsowork.Thispermissionwillhavetobemanuallyenabledbytheusersfromsettings.Finally…Foractualcodesamples,youcanheadontothebelowresourcesforaclearerunderstanding.Preparingforscopedstorage(AndroidDevSummit‘19)StorageaccesswithAndroid11What’snewinsharedstorage(GoogleI/O’19)Filesformiles:Wheretostorethemall?(AndroidDevSummit‘18)AndroidDevelopersGuideIhavealsocreatedasampleapponGitHubwhichimplementssomefileoperationsonInternalandSharedstorage.YoucantrythisappondifferentAPIlevelstoseehowdifferentoperationsareaffected.Hopethisarticlehelpedingettingagistofchangesandhopedyoulearnedsomething!ProAndroidDevThelatestpostsfromAndroidProfessionalsandGoogleDeveloperExpertsFollow73AndroidStorageScopedStorageAndroid10Android1173 claps73WrittenbyNikhilBansalFollowAndroidEngineer@ZetaSuiteFollowProAndroidDevFollowThelatestpostsfromAndroidProfessionalsandGoogleDeveloperExperts.FollowWrittenbyNikhilBansalFollowAndroidEngineer@ZetaSuiteProAndroidDevFollowThelatestpostsfromAndroidProfessionalsandGoogleDeveloperExperts.MoreFromMediumRecyclerViewwithGridLayoutinKotlinPasinduLaksaraBackgroundworkinAndroid :WorkManager .marouenekhadhraouiGettingSet-UpforAndroidDevelopmentinUnityMatthewClarkKotlin:KnownUnknownsAnvithBhatinProAndroidDevAwayofusingAndroidDataBindingLibraryinformpagesFurkanAşkıninOzanSuperAppDay35 — Improvements:thrusterUIelementConnorFullartonStackWidgetinFlutterMeetpanchalinQuickCodeUnitTestingInFlutterWithMockitoApoorvWadhwainFlutterDevs



請為這篇文章評分?