Internal or External storage for editable app-specific file
文章推薦指數: 80 %
Internal storage is best when you want to be sure that neither the user nor other apps can access your files. External storae : External storage ...
Home
Public
Questions
Tags
Users
Collectives
ExploreCollectives
FindaJob
Jobs
Companies
Teams
StackOverflowforTeams
–Collaborateandshareknowledgewithaprivategroup.
CreateafreeTeam
WhatisTeams?
Teams
CreatefreeTeam
CollectivesonStackOverflow
Findcentralized,trustedcontentandcollaboratearoundthetechnologiesyouusemost.
Learnmore
Teams
Q&Aforwork
Connectandshareknowledgewithinasinglelocationthatisstructuredandeasytosearch.
Learnmore
InternalorExternalstorageforeditableapp-specificfile
AskQuestion
Asked
3years,5monthsago
Active
3years,5monthsago
Viewed
2ktimes
2
Iamcreatinganappthatrequieranapp-specificfilethatIcalled"conf.cfg"forexample.thisfileneedtobereadbymyapplicationtocreatesomeobjectetc...thebodyofthefilelooklikethat:
#activation,level,type,regexorarray
0,"critic",0,"\\d{4}\\w\\d{3}"
1,"critic",1,[word1,word2]
1,"minor",0,"\\d{2}-\\w{3}-\\d{4}\\s?\\/?\\s?\\d{2}:\\d{2}"
DoingmyresearchIfoundthatthereistwotypeofStorageinandroid:
Internalstorage:
Internalstorageisbestwhenyouwanttobesurethatneithertheusernorotherappscanaccessyourfiles.
Externalstorae:
Externalstorageisthebestplaceforfilesthatdon'trequireaccessrestrictionsandforfilesthatyouwanttosharewithotherappsorallowtheusertoaccesswithacomputer.
AsIwanttheusertobeabletoedit/download/upload/USEthisfile,ExternalStorageseemstobeagoodchoice.HoweveronDevelopperAndroidtheysaid:
Caution:TheexternalstoragemightbecomeunavailableiftheuserremovestheSDcardorconnectsthedevicetoacomputer.AndthefilesarestillvisibletotheuserandotherappsthathavetheREAD_EXTERNAL_STORAGEpermission.Soifyourapp'sfunctionalitydependsonthesefilesoryouneedtocompletelyrestrictaccess,youshouldinsteadwriteyourfilestotheinternalstorage.
Caution:Filesonexternalstoragearenotalwaysaccessible,becauseuserscanmounttheexternalstoragetoacomputerforuseasastoragedevice.Soifyouneedtostorefilesthatarecriticaltoyourapp'sfunctionality,youshouldinsteadstorethemoninternalstorage.
Asthisfileneedtobealwaysavailableandiscriticaltomyapp'sfunctionality
So...InternalStorageseemstobebetter.ButIneedtheusertoseeandbeabletousethefile.AndhereI'mstuck.
Anyonehasanideaofwhereandhowtoput/createthisfile?
EDIT:following@greenappsanswer
heerisapieceofcodeI'vewrote.IusethegetExternalFilesDir(null)commandtowriteandstoremyfile
StringfolderName="Innovation";
StringconfFileName="conf.txt";
StringcommentSymbol="#";
StringcommentLine=commentSymbol+"activation,level,type,regexorarray";
Filestorage=getExternalFilesDir(null);
Filefolder=newFile(storage,folderName);
Fileconf=newFile(folder,confFileName);
Log.d(TAG,"Folderaction!");
if(!folder.exists()){
if(folder.mkdirs()){
Log.d(TAG,"Created:"+folder.getAbsolutePath());
}else{
Log.e(TAG,"foldernotcreated!");
}
}
Log.d(TAG,"Fileaction!");
if(!conf.exists()){
try{
Log.d(TAG,"opening...");
FileOutputStreamfos=newFileOutputStream(conf);
fos.write(commentLine.getBytes());
fos.close();
Log.d(TAG,"Created:"+conf.getAbsolutePath());
}catch(IOExceptione){
e.printStackTrace();
}
}
if(conf.exists()){
Log.d(TAG,"Fileexistat:"+conf.getAbsolutePath());
}
thefileiscreated,asshownbythelastlog
Created:/storage/emulated/0/Android/data/com.aralex.innovation/files/Innovation/conf.txt
ButwhenIsearchthefilewiththenativefileexplorerapplicationofthephone,Ican'tfindit.Icangotothefilefolderbutthefolder"Innovation/"ishidden.
ThisisaproblembecauseIwantthefiletobevisible.
Phone:Samsungs7,s7edge,s9+
DefaultFileExplorerIcon
DefaultFileExplorerOppened
androidfilestorage
Share
Follow
editedJun4'18at9:03
ArnauldAlex
askedJun1'18at8:34
ArnauldAlexArnauldAlex
30911goldbadge22silverbadges1313bronzebadges
14
Internalorexternalstoragearebothpossible.Alldeviceshaveit.TheyhavenothingtodowitharemovablemicroSDcard.Youreadedanolddoc.
– greenapps
Jun1'18at8:42
butitsays:TheexternalstoragemightbecomeunavailableiftheuserremovestheSDcardorconnectsthedevicetoacomputer
– ArnauldAlex
Jun1'18at8:44
Whatisunclearinyourstoryishowthisfilewouldlandonmydeviceifiinstalledyourapponit.
– greenapps
Jun1'18at8:45
Yesyoureadedanolddoc.Externalstorageisalwaysavailable.Pleaserereadmycomment.
– greenapps
Jun1'18at8:46
Ifit'sthefirstinstallationthenthefileiscreatedwithonlythefirstline(theonethatbeginsby"#").Thenuserhavetobeabletoeditthisfile(withanotherapponhisphone,likeasimplenotepad)orbeabletogowithhisphonefileexplorertothefile,replaceitetc...likeafilesimplefileonacomputer
– ArnauldAlex
Jun1'18at8:48
|
Show9morecomments
2Answers
2
Active
Oldest
Votes
1
WellIfinallyfoundananswermyself.
OnthispostAndroidcreatefoldersinInternalMemory@prodevspecifythatEnvironment.getExternalStorageDirectory()isagoodplace,becausethefilewillbeaccessibleand:
notethatExternalStorageinEnvironment.getExternalStorageDirectory()doesnotnecessarilyreferstosdcard,itreturnsphoneprimarystoragememory
itrequirespermissions(onlyforbuildversion>=M):
延伸文章資訊
- 1Access app-specific files | Android Developers
If internal storage doesn't provide enough space to store app-specific files, consider using exte...
- 2Switching to app-specific storage - SurveyCTO Documentation
In Admin Settings, there is a new option at the bottom labeled App storage location. You can swit...
- 3How to Correctly Store App-Specific Files in Android
App-specific files can be stored internally or externally (on the sd card) and the Android API he...
- 4Internal or External storage for editable app-specific file
Internal storage is best when you want to be sure that neither the user nor other apps can access...
- 5Data and file storage overview | Android Developers
App-specific storage: Store files that are meant for your app's use only, either in dedicated dir...