亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

Android對sdcard擴(kuò)展卡文件操作實(shí)例詳解

 更新時(shí)間:2014年07月22日 09:43:10   投稿:shichen2014  
這篇文章主要介紹了Android對sdcard擴(kuò)展卡文件操作,非常實(shí)用的技術(shù),需要的朋友可以參考下

Android對sdcard擴(kuò)展卡文件的操作其實(shí)就是普通的文件操作,但是仍然有些地方需要注意。比如:

1.加入sdcard操作權(quán)限;

2.確認(rèn)sdcard的存在;

3.不能直接在非sdcard的根目錄創(chuàng)建文件,而是需要先創(chuàng)建目錄,再創(chuàng)建文件

實(shí)例如下:

(1)在AndroidManifest.xml添加sdcard操作權(quán)限

<!-- sdcard權(quán)限 -->
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"></uses-permission>

(2)變量聲明:

private final static String PATH = "/sdcard/digu";
private final static String FILENAME = "/notes.txt";

(3)向sdcard寫文件:

/**
* 寫文件
*/
private void onWrite() {
   try {
     Log.d(LOG_TAG, "Start Write");
     //1.判斷是否存在sdcard
     if (Environment.MEDIA_MOUNTED.equals(Environment
         .getExternalStorageState())) {
       //目錄
       File path = new File(PATH);
       //文件
       File f = new File(PATH + FILENAME);
       if(!path.exists()){
         //2.創(chuàng)建目錄,可以在應(yīng)用啟動(dòng)的時(shí)候創(chuàng)建
         path.mkdirs();
       }
       if (!f.exists()) {
         //3.創(chuàng)建文件
         f.createNewFile();
       }
       OutputStreamWriter osw = new OutputStreamWriter(
           new FileOutputStream(f));
       //4.寫文件,從EditView獲得文本值
       osw.write(editor.getText().toString());
       osw.close();
     }
   } catch (Exception e) {
     Log.d(LOG_TAG, "file create error");
   }
 }

相關(guān)文章

最新評論