欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

操作SD卡中文件夾和文件的方法

 更新時(shí)間:2013年04月16日 22:05:11   作者:  
操作SD卡中文件夾和文件的方法,需要的朋友可以參考一下

文件夾的創(chuàng)建

復(fù)制代碼 代碼如下:

        File file = Environment.getExternalStorageDirectory();
        File file_0 = new File(file, "file_demo");
          if (!file_0.exists()) {
              file_0.mkdirs();
           }


 創(chuàng)建文件夾的時(shí)候,需要<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />權(quán)限,

        否則會報(bào)如下錯(cuò)誤:

ApplicationContext Unable to create external files directory

 這里建議使用mkdirs()創(chuàng)建文件夾,而不是用mkdir(),因?yàn)榍罢呖梢酝瑫r(shí)創(chuàng)建父文件夾,如果不存在的話,而后者不能。

文件的創(chuàng)建      

復(fù)制代碼 代碼如下:

                     File file = Environment.getExternalStorageDirectory();
                      File file_0 = new File(file, "pic");
                         if (!file_0.exists()) {
                                file_0.mkdirs();
                         }
                      try {
                          File pic = new File(file_0, "pic.png");
                      InputStream is = getResources().openRawResource(
                                                            R.drawable.ic_launcher);
                      OutputStream os = new FileOutputStream(pic);
                      byte[] data = new byte[is.available()];
                      is.read(data);
                      os.write(data);
                      is.close();
                      os.close();
                      } catch (FileNotFoundException e) {
                         // TODO Auto-generated catch block
                      e.printStackTrace();
                      } catch (IOException e) {
                       // TODO Auto-generated catch block
                             e.printStackTrace();
                      }


創(chuàng)建的文件名不能帶有.后綴的,否則會報(bào)如下錯(cuò)誤:

java.io.FileNotFoundException:/mnt/sdcard/pic/pic.png (Is a directory)

同時(shí)在對文件夾的讀寫操作時(shí)最好添加如下權(quán)限:

復(fù)制代碼 代碼如下:

 <uses-permission android:name="android.permission.MOUNT_UNMOUNT_FILESYSTEMS" />
 

相關(guān)文章

最新評論