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

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

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

文件夾的創(chuàng)建

復制代碼 代碼如下:

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


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

        否則會報如下錯誤:

ApplicationContext Unable to create external files directory

 這里建議使用mkdirs()創(chuàng)建文件夾,而不是用mkdir(),因為前者可以同時創(chuàng)建父文件夾,如果不存在的話,而后者不能。

文件的創(chuàng)建      

復制代碼 代碼如下:

                     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)建的文件名不能帶有.后綴的,否則會報如下錯誤:

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

同時在對文件夾的讀寫操作時最好添加如下權限:

復制代碼 代碼如下:

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

相關文章

最新評論