Android編程之在SD卡上進(jìn)行文件讀寫(xiě)操作實(shí)例詳解
本文實(shí)例講述了Android編程之在SD卡上進(jìn)行文件讀寫(xiě)操作的方法。分享給大家供大家參考,具體如下:
很多知識(shí)只有真正理解掌握之后才能運(yùn)用自如,舉一反三。對(duì)Java中的文件操作和android系統(tǒng)SD卡里面的文件操作,你覺(jué)得有區(qū)別嗎,顯然沒(méi)有本質(zhì)區(qū)別,如果勉強(qiáng)說(shuō)有,那也是不足為道滴,但我們?cè)趯?shí)際運(yùn)用中卻要注意如下幾點(diǎn),不然問(wèn)題會(huì)纏上你。
1、首先想要對(duì)android系統(tǒng)SD卡里文件操作需要添加使用權(quán)限:
android系統(tǒng)是不會(huì)讓外來(lái)程序隨意動(dòng)自己內(nèi)存的,如果沒(méi)有許可證,不好意思,不準(zhǔn)你動(dòng)我地盤(pán),在我地盤(pán)得聽(tīng)我的。在配置文件里AndroidManifest.xml里面添加SD卡讀寫(xiě)數(shù)據(jù)權(quán)限。
2、獲取文件絕對(duì)路徑:
這里有兩種方法
方法1:直接使用/mnt/sdcard/+文件名
如: final String FILE_NAME = "/mnt/sdcard/00000.H264";
方法2:通過(guò)系統(tǒng)提供的方法獲取SD的路徑,然后在后面加上文件名稱。這個(gè)方法有點(diǎn)啰嗦,暫沒(méi)感受到它的妙處,不過(guò)在此還是羅列出來(lái),供以后參考。見(jiàn)后面實(shí)例。
3、獲取文件路徑后的操作,跳不出對(duì)FileInputStream、FileOutputStream、FileReader、FileWriter四個(gè)類的使用。具體參看前面《Java編程之文件讀寫(xiě)》
下附將數(shù)據(jù)寫(xiě)入SD卡文件的簡(jiǎn)單運(yùn)用代碼:
方法1運(yùn)用代碼:
public class FileTestActivity extends Activity { final String FILE_NAME = "/mnt/sdcard/007.test"; public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); Button btn = (Button) findViewById(R.id.button1); final byte[] buf = { 0 }; btn.setOnClickListener(new OnClickListener() { public void onClick(View arg0) { try { FileOutputStream fout = new FileOutputStream(FILE_NAME, true); BufferedOutputStream bout = new BufferedOutputStream(fout); bout.write(buf); bout.flush(); bout.close(); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } } }); } }
方法2運(yùn)用實(shí)例代碼
public class FileTestActivity extends Activity { final String FILE_NAME = "/007.test"; public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); Button btn = (Button) findViewById(R.id.button1); final byte[] buf = { 0 }; btn.setOnClickListener(new OnClickListener() { public void onClick(View arg0) { if (Environment.getExternalStorageState().equals( Environment.MEDIA_MOUNTED)) { File sdDir = Environment.getExternalStorageDirectory(); System.out.println(sdDir); FileOutputStream fout; try { System.out.println(sdDir.getCanonicalPath() + FILE_NAME); fout = new FileOutputStream(sdDir.getCanonicalPath() + FILE_NAME, true); BufferedOutputStream bout = new BufferedOutputStream( fout); bout.write(buf); bout.flush(); bout.close(); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } } } }); } }
希望本文所述對(duì)大家Android程序設(shè)計(jì)有所幫助。
相關(guān)文章
Android保持屏幕常亮2種實(shí)現(xiàn)方法
這篇文章主要介紹了Android保持屏幕常亮2種實(shí)現(xiàn)方法,本文分別給出示例代碼,需要的朋友可以參考下2015-05-05實(shí)例講解Android多線程應(yīng)用開(kāi)發(fā)中Handler的使用
這篇文章主要介紹了Android多線程應(yīng)用開(kāi)發(fā)中Handler的使用,Handle主要被用來(lái)更新UI和處理消息,需要的朋友可以參考下2016-01-01Android中AlarmManager+Notification實(shí)現(xiàn)定時(shí)通知提醒功能
本篇文章主要介紹了Android中AlarmManager+Notification實(shí)現(xiàn)定時(shí)通知提醒功能,非常具有實(shí)用價(jià)值,需要的朋友可以參考下2017-10-10Android?App隱私合規(guī)檢測(cè)輔助工具Camille詳解
Camille是一款A(yù)ndroid?App隱私合規(guī)檢測(cè)輔助工具,現(xiàn)如今APP隱私合規(guī)十分重要,各監(jiān)管部門(mén)不斷開(kāi)展APP專項(xiàng)治理工作及核查通報(bào),不合規(guī)的APP通知整改或直接下架,下面小編給大家介紹下Android?App隱私合規(guī)檢測(cè)輔助工具Camille,感興趣的朋友一起看看吧2022-02-02Android實(shí)現(xiàn)長(zhǎng)按圓環(huán)動(dòng)畫(huà)View效果的思路代碼
這篇文章主要介紹了Android實(shí)現(xiàn)長(zhǎng)按圓環(huán)動(dòng)畫(huà)View效果,本文給大家分享實(shí)現(xiàn)思路,通過(guò)示例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-09-09解決Android studio xml界面無(wú)法預(yù)覽問(wèn)題
這篇文章主要介紹了解決Android studio xml界面無(wú)法預(yù)覽問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2020-03-03android studio git 刪除已在遠(yuǎn)程倉(cāng)庫(kù)的文件或文件夾方式
這篇文章主要介紹了android studio git 刪除已在遠(yuǎn)程倉(cāng)庫(kù)的文件或文件夾方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2020-04-04android開(kāi)發(fā)去除標(biāo)題欄的方法
這篇文章主要介紹了android開(kāi)發(fā)去除標(biāo)題欄的方法,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2021-04-04Android實(shí)現(xiàn)上拉加載更多ListView(PulmListView)
這篇文章主要介紹了Android實(shí)現(xiàn)上拉加載更多ListView:PulmListView,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2016-09-09