Android Internal and External storage 讀寫檔案 ... - 隨意窩

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

http://blog.tonycube.com/2012/03/android-internal-and-external-storage.html1. 存在手機還是SDcardAndroid可以將檔案儲存在手機上的記憶體(Internal Storage), ... IceCream的程式設計網路筆記這裡記錄了我的點點滴滴!日誌相簿影音好友名片 201209071452AndroidInternalandExternalstorage讀寫檔案?Androidhttp://blog.tonycube.com/2012/03/android-internal-and-external-storage.html1.存在手機還是SDcardAndroid可以將檔案儲存在手機上的記憶體(InternalStorage),或是外部儲存媒體SDcard(ExternalStorage)。

Internalstorage是儲存在/data/data/package_name/files目錄中,每個App會有一個獨立的目錄來儲存檔案,其他App無法存取。

由於是儲存在手機的記憶體中,所有會有空間的限制,不建議在這裡儲存容量太大的檔案。

使用Internalstorage不需要額外設定權限。

使用ExternalStorage則必須設定讀取SDcard的權限:儲存目錄為/sdcard,容量則取決於SDcard本身的容量。

儲存在Internalstorage時,當App被移除,所有的資料也會被移除,而Externalstorage因為是儲存在SDcard,所以不會因為App被移除而消失。

2.使用方法兩者使用方式基本上都相同,只是在讀寫時會使用不同的類別。

Internalstorage://讀檔FileInputStreamfin=this.openFileInput(filename);//寫檔FileOutputStreamfout=this.openFileOutput(filename,Context.MODE_PRIVATE);對Internalstorage做讀寫時是使用android.content.ContextWrapper類別中的方法,this是指該Activity。

Context.MODE_PRIVATE是指該檔案為私有的,只能由該App讀寫。

另外還有其他指示性的常數,在Context抽象類別可以找到以MODE_開頭的常數。

Externalstorage://取得外部儲存媒體的目錄(這裡會是/sdcard)Stringpath=Environment.getExternalStorageDirectory().getPath();//檔案路徑,記得要加斜線(這樣/sdcard/filename)Filefile=newFile(path+"/"+filename);//讀檔FileInputStreamfin=newFileInputStream(file);//寫檔FileOutputStreamfout=newFileOutputStream(file);基本上就是使用原本的Java.io裡的類別。

這樣檔案會直接儲存在SDcard下,或許你會想要建立一個自己App使用的目錄,使用的方式如下://先取得sdcard目錄Stringpath=Environment.getExternalStorageDirectory().getPath();//利用File來設定目錄的名稱(myappdir)Filedir=newFile(path+"/myappdir");//先檢查該目錄是否存在if(!dir.exists()){//若不存在則建立它dir.mkdir();}如此就會有一個/sdcard/myappdir的目錄產生。

之後你的檔案就可以放在Filefile=newFile(path+"/myappdir/"+filename);此外,在使用Externalstorage之前,最好先檢查一下外部媒是否存在及能否讀寫。

//取得外部儲存媒體的狀態Stringstate=Environment.getExternalStorageState();//判斷狀態if(Environment.MEDIA_MOUNTED.equals(state)){Log.d(TAG,"可以讀寫");}elseif(Environment.MEDIA_MOUNTED_READ_ONLY.equals(state)){Log.d(TAG,"只可以讀取,無法寫入");}else{Log.d(TAG,"無法讀寫");}3.程式碼我把實際使用的程式碼寫成方法,方便使用,請依自己的需求修改,如下:Internalstorage://寫檔privatevoidwriteDataToFile(Stringfilename,Stringdata){try{FileOutputStreamfout=this.openFileOutput(filename,Context.MODE_PRIVATE);fout.write(data.getBytes());fout.close();}catch(FileNotFoundExceptione){e.printStackTrace();}catch(IOExceptione){e.printStackTrace();}}//讀檔privateStringreadDataFromFile(Stringfilename){Stringresult=null;try{StringBuildersb=newStringBuilder();FileInputStreamfin=this.openFileInput(filename);byte[]data=newbyte[fin.available()];while(fin.read(data)!=-1){sb.append(newString(data));}fin.close();result=sb.toString();}catch(FileNotFoundExceptione){e.printStackTrace();}catch(IOExceptione){e.printStackTrace();}returnresult;}Externalstorage://寫檔到sdcardprivatevoidwriteToSDcard(Stringfilename,Stringdata){//建立自己的目錄Stringpath=Environment.getExternalStorageDirectory().getPath();Filedir=newFile(path+"/movietime");if(!dir.exists()){dir.mkdir();}try{Filefile=newFile(path+"/movietime/"+filename);FileOutputStreamfout=newFileOutputStream(file);fout.write(data.getBytes());fout.close();}catch(FileNotFoundExceptione){e.printStackTrace();}catch(IOExceptione){e.printStackTrace();}Log.d(TAG,"WritetoSDCARD!");}//從sdcard讀檔privateStringreadFromSDcard(Stringfilename){Stringpath=Environment.getExternalStorageDirectory().getPath();Filefile=newFile(path+"/movietime/"+filename);StringBuildersb=newStringBuilder();try{FileInputStreamfin=newFileInputStream(file);byte[]data=newbyte[fin.available()];while(fin.read(data)!=-1){sb.append(newString(data));}fin.close();}catch(FileNotFoundExceptione){e.printStackTrace();}catch(IOExceptione){e.printStackTrace();}Log.d("TAG","ReadfromSDCARD:"+json.toString());returnsb.toString();}IceCream/Xuite日誌/回應(0)/引用(0)沒有上一則|日誌首頁|沒有下一則回應 加我為好友日誌相簿影音 我的相簿 uwlib_mud's新文章02Unity操作介面03Unity專案管理04Unity物件編輯05Unity地形產生器06Unity樹木產生器07Unity環境設定08Unity光源設定09匯入3D模型檔案10創造3D遊戲角色11Unity材質設定 全部展開|全部收合 uwlib_mud's新回應沒有新回應! 關鍵字



請為這篇文章評分?