Android 10 & 11 storage cheat sheet | by Petros Douvantzis

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

It can read files created by other apps. You can't write to a random folder in the internal storage such as /storage/emulated/0/Test but you can ... GetstartedOpeninappPetrosDouvantzisSigninGetstarted144FollowersAboutGetstartedOpeninappAndroid10&11storagecheatsheetPetrosDouvantzisMar4·3minreadGooglekeepschangingthewayappscanaccesssharedfilesbyaddingandliftingrestrictionstoexistingAPIstothepointtheyhadtowritethis.ThefollowingisasimplifiedguideaboutthestorageoptionsofanappandhowtheydifferentiatebetweenAndroidversions.TLDR:notmuchhaveactuallychangedifyou’remakingacameraapp.Ifyouhaveagalleryorfileapp,thenyouhavetodosomerefactoring.InAndroid,anapphasaccessto:privatefoldersthatonlytheappcansee:getFilesDir(),getCacheDir()./data/user/0//files/data/user/0/c/filesapp-specificfoldersthatotherappscansee.Thereisagenericone:getExternalFilesDirs()andamediaone:getExternalMediaDirs().Thesearedeletedwhentheappisdeleted.Thesedirectorieshave2variants:1.thosethatexistininternalstorage.Youcangetthepathusingthefirstelementinthearrayreturnedbytheaforementionedmethods./storage/emulated/0/Android/data//files/storage/emulated/0/Android/media/2.thosethatexistinexternalstoragesuchasSDcard.Youcangetthepathusingthesecondelementinthearrayreturnedbytheaforementionedmethods./storage/11EC-2402/Android/data//files/storage/11EC-2402/Android/media/publicfoldersthatallappscansee,likeDCIM,Music,Downloadfolderetc.TheycanbeselectedbytheuserbystartinganACTION_OPEN_DOCUMENT_TREEintent.Thesedirectorieshave2variants:1.thosethatexistininternalstorage.Another(deprecated)waytogetthepathtothesefoldersiswithgetExternalStoragePublicDirectory()./storage/emulated/0/DCIM/storage/emulated/0/Download2.thosethatexistinexternalstoragesuchasSDcard./storage/11EC-2402/DCIMNotethatthepathsaboveareexamplesandshouldnotbeusedasis.AllAndroidversionscanwriteto1,2.1,2.2usingtheclassicFilesAPI.Android10requiresaskinguserpermissionforWRITE_EXTERNAL_STORAGE,asusual,andsettingandroid:requestLegacyExternalStorage=”true”tothemanifesttoaccess3.1usingFilesAPI.Youcanwritewhateverfiletypeyouwantinthesefoldersandalsodeletepre-existingfilesorfilescreatedbyotherapps.3.2can’tbeaccessedthisway.Android11canwriteto3.1and3.2(nomanifestflagneeded)aslongasthefiletypewrittenmatchesthedirectory.Forexample,youcan’twritea.mp3fileinDCIMfolder.InDownloadsfolder,youcanwritewhateveryouwant.Yourappcandeleteonlyfilescreatedbyit.Itcan’tdeletepre-existingfilesorfilecreatedbyapreviousinstallationoftheapp.Itcanreadfilescreatedbyotherapps.Youcan’twritetoarandomfolderintheinternalstoragesuchas/storage/emulated/0/Testbutyoucanreaditscontents.NotethatinAndroid11youdon’tevenhavetodeclareWRITE_EXTERNAL_STORAGEinyourmanifestoraskuserpermissioninordertowritetoalltheaforementionedfolders.Ifyouwanttoreadfilescreatedbyotherappsthough,you’llhavetoasktheuserforWRITE_EXTERNAL_STORAGEorREAD_EXTERNAL_STORAGEpermission.Andhere’sanothersmallcheatsheettakenfromhere:Android11==codeR==API30Android10==codeQ==API29PetrosDouvantzisAndroid&formeriOSdeveloperhttps://horizon.camera/Follow4646 46AndroidStorageStorageAccessFrameworkMorefromPetrosDouvantzisFollowAndroid&formeriOSdeveloperhttps://horizon.camera/MoreFromMediumHowtousemultipleusersatthesametimeinOxygenOSZepsecDay113 — PublishingourAppBobWeisharTopAndroidStudioShortcutsMustufaAnsariinBetterProgrammingASimpleNote-TakingAppWithFlutter — Part1MustufaAnsariinBetterProgrammingAndroidDevelopmentforBeginnersSatyaPavanKantamaniBackgroundBLEscaninDOZEmodeonAndroiddevicesElvinaShinProAndroidDevAndroidTVnowsupportsaudiostreaminginthebackgroundforChromecastappsGlobebusinesscenterAndroidShimmerEffectwithRecyclerviewAchmadQomarudin



請為這篇文章評分?