The absolute path of internal storage folder is: /data/data//files/ . Call getFilesDir() method to obtain this path.
HomeBlogPostsandroid1WhatIsAndroidInternalStorage
Androidallowsyourandroidapplicationsaveorcacheprivatedataorfilesonthedevicememory,whichiscalledinternalstorage.
Datasavedorcachedininternalstorageareprivateandcanbeaccessedonlywithinyourapplication,otherapplicationscannotaccessthem.
Furthermore,dataofyourapplicationthatissavedininternalstoragewillbealsodeletedifuserremoveyourapplication.
2APIs
Theabsolutepathofinternalstoragefolderis:/data/data//files/.CallgetFilesDir()methodtoobtainthispath.getFilesDir()willreturnajava.io.Fileobject.ThenyoucansimplyusejavaIOAPIstoreadorwritefilesunderinternalstoragefolder.
2.1Write
Wewillcreateasimpledemoapplicationtowriteafiletointernalstorage.
MainUIcontrolshere:
AButton,clicktowritefile;
AnotherButton,clicktoreadsavedfile;
ATextViewtodisplaytheabsolutefilepathofinternalstorage;
Thefiletobesavednamedhello.txt,andwe'llwriteastring"HelloWorld!"toit.
packagecom.codevoila.androidtutorial;
importandroid.support.v7.app.AppCompatActivity;
importandroid.os.Bundle;
importandroid.view.View;
importandroid.widget.Button;
importandroid.widget.TextView;
importandroid.widget.Toast;
importjava.io.File;
importjava.io.FileOutputStream;
importjava.io.IOException;
publicclassInternalStorageActivityextendsAppCompatActivityimplementsView.OnClickListener{
privateTextViewtextViewPathHint;
privateButtonbtnWriteInternal;
privateStringdemoFileName="hello.txt";
privateStringdemoFileContent="HelloWorld!";
@Override
protectedvoidonCreate(BundlesavedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_internal_storage);
textViewPathHint=(TextView)findViewById(R.id.tv_path_hint);
btnWriteInternal=(Button)findViewById(R.id.btn_write_internal);
btnWriteInternal.setOnClickListener(this);
}
@Override
publicvoidonClick(Viewv){
switch(v.getId()){
caseR.id.btn_write_internal:
this.writeInternalDemo();
break;
default:
break;
}
}
privatevoidwriteInternalDemo(){
//Pathwillbe:/data/data//hello.txt
Filefile=newFile(getFilesDir(),this.demoFileName);
//DisplaytheabsolutepathofInternalStorage
this.textViewPathHint.setText(file.getAbsolutePath());
FileOutputStreamfos=null;
try{
fos=newFileOutputStream(file);
fos.write(this.demoFileContent.getBytes());
}catch(Exceptione){
e.printStackTrace();
Toast.makeText(this,"Failed:"+e.getMessage(),Toast.LENGTH_LONG).show();
}finally{
if(fos!=null){
try{
Toast.makeText(this,"Writeto"+demoFileName+"successfully!",Toast.LENGTH_LONG).show();
fos.close();
}catch(IOExceptione){
e.printStackTrace();
}
}else{
Toast.makeText(this,"Failedtowrite!",Toast.LENGTH_LONG).show();
}
}
}
}
2.2Read
Readingfilefrominternalstorageisalsosimple,justlikethecommonJavaIOoperations.
We'lladdareadbuttoninabovedemoapplication.Afterclickingreadbutton,apopupToastwilldisplaythecontentreadfrominternalstoragefile.
packagecom.codevoila.androidtutorial;
importandroid.support.v7.app.AppCompatActivity;
importandroid.os.Bundle;
importandroid.view.View;
importandroid.widget.Button;
importandroid.widget.TextView;
importandroid.widget.Toast;
importjava.io.BufferedReader;
importjava.io.File;
importjava.io.FileInputStream;
importjava.io.FileOutputStream;
importjava.io.IOException;
importjava.io.InputStreamReader;
publicclassInternalStorageActivityextendsAppCompatActivityimplementsView.OnClickListener{
privateTextViewtextViewPathHint;
privateButtonbtnWriteInternal;
privateButtonbtnReadInternal;
privateStringdemoFileName="hello.txt";
privateStringdemoFileContent="HelloWorld!";
@Override
protectedvoidonCreate(BundlesavedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_internal_storage);
textViewPathHint=(TextView)findViewById(R.id.tv_path_hint);
btnWriteInternal=(Button)findViewById(R.id.btn_write_internal);
btnWriteInternal.setOnClickListener(this);
btnReadInternal=(Button)findViewById(R.id.btn_read_internal);
btnReadInternal.setOnClickListener(this);
}
@Override
publicvoidonClick(Viewv){
switch(v.getId()){
caseR.id.btn_write_internal:
this.writeInternalDemo();
break;
caseR.id.btn_read_internal:
this.readInternalDemo();
break;
default:
break;
}
}
privatevoidwriteInternalDemo(){
//...
}
privatevoidreadInternalDemo(){
Filefile=newFile(getFilesDir(),this.demoFileName);
if(!file.exists()){
this.btnReadInternal.setEnabled(false);
Toast.makeText(this,"Failed:filedoesnotexist",Toast.LENGTH_LONG).show();
return;
}
FileInputStreamfis=null;
StringtextContent="";
try{
fis=newFileInputStream(file);
BufferedReaderbr=newBufferedReader(newInputStreamReader(fis));
textContent=br.readLine();
}catch(Exceptione){
Toast.makeText(this,"Failed:"+e.getMessage(),Toast.LENGTH_LONG).show();
}finally{
if(fis!=null){
Toast.makeText(this,"ReadSuccessfully:"+textContent,Toast.LENGTH_LONG).show();
try{
fis.close();
}catch(IOExceptione){
e.printStackTrace();
Toast.makeText(this,"Failedtoread!",Toast.LENGTH_LONG).show();
}
}
}
}
}
2.3Cache
InsteadofgetFilesDir(),getCacheDir()willreturnaninternalstoragefolderpathforcachingdata.TheabsolutepathreturnedbygetCacheDir()is:/data/data//cache/.Thisfolderisusedtocachesometemporaryfilesinyourapplication.
Thereareimportantdifferencesbetweenfilesfolderandcachefolderininternalstorage.
ThedatasavedundercachefolderwillbedeletedautomaticallybyAndroidsystemifthesparespaceofinternalstorageisnotenoughorfull;
ThedatasavedunderfilesfolderwillNOTbedeletedunlessdeviceuserclearitexplicitly.
AndroidprovidedUIinterfacetoclearcacheordataofaninstalledapplication.InyourAndroiddeviceoremulator,navigatetoSettings>Apps>YOUR_APP>storageyoucanseetwoclearbuttons.
ClearCache:willdeletealldataunder/data/data//cache/;
ClearData:willdeleteallcustomdataunderyourapplicationfolder,includingBOTH/data/data//cache/AND/data/data//files/;
ItisabestpracticetosavetheimportantdataofyourapplicationintothepaththatisreturnedbygetFilesDir().
android