Storage Model | Android 101 - ProAndroidDev
文章推薦指數: 80 %
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
延伸文章資訊
- 1How to I read and write a txt file from android 10 and above ...
use context.getExternalFilesDir() or context.getExternalCacheDir() ,these also work in 10.
- 2Access media files from shared storage | Android Developers
On devices that run Android 10 (API level 29) and higher, these files are ... You can read the co...
- 3Handling Files in Code After Android 10 Released - Medium
file API in Android 10 if the Storage Access Framework does not suit your needs practically or op...
- 4Media Store access using openFile() on Android 10 - Joe Birch
In Android. October 9, 2019. 6 Min Read. M. As of Android 10, things have changed slightly with h...
- 5Android File IO Tutorial with Internal and External Storage
No permissions are required to read or write files to internal storage for your ... For apps the ...