android下建立檔案夾和修改其許可權的方法

來源:互聯網
上載者:User

   由於工作的需要,今天研究了在android下建立檔案夾和修改其許可權的方法,需要瞭解的是每個應用程式套件組合都會有一個私人的儲存資料的目錄(類似檔案夾),只有屬於該包的應用程式才能寫入該目錄空間,每個包應用程式的私人資料目錄位 於Android絕對路徑/data/data/<包名>/目錄中。除了私人資料目錄應用程式還擁有/sdcard目錄(即SD Card的寫入許可權,但不可以修改sd card下檔案的存取權限)。檔案系統中其他系統目錄,第三方應用程式是不可寫入的。

       代碼如下兩種:

1、

//建立檔案夾

File destDir = new File(“/data/data/[your path]/temp”);
  if (!destDir.exists()) {
   destDir.mkdirs();
  }

//修改許可權

 FileOutputStream fos;   

 fos = openFileOutput("filename" , MODE_WORLD_READABLE);  


 

備忘:可用的mode 參數如下:

    /**
     * File creation mode: the default mode, where the created file can only
     * be accessed by the calling application (or all applications sharing the
     * same user ID).
     * @see #MODE_WORLD_READABLE
     * @see #MODE_WORLD_WRITEABLE
     */
    public static final int MODE_PRIVATE = 0x0000;
    /**
     * File creation mode: allow all other applications to have read access
     * to the created file.
     * @see #MODE_PRIVATE
     * @see #MODE_WORLD_WRITEABLE
     */
    public static final int MODE_WORLD_READABLE = 0x0001;
    /**
     * File creation mode: allow all other applications to have write access
     * to the created file.
     * @see #MODE_PRIVATE
     * @see #MODE_WORLD_READABLE
     */
    public static final int MODE_WORLD_WRITEABLE = 0x0002;
    /**
     * File creation mode: for use with {@link #openFileOutput}, if the file
     * already exists then write data to the end of the existing file
     * instead of erasing it.
     * @see #openFileOutput
     */
    public static final int MODE_APPEND = 0x8000;


 

2、

//建立檔案夾

File destDir = new File(“/data/data/[your path]/temp”);
  if (!destDir.exists()) {
   destDir.mkdirs();
  }

Process p;
int status;
            try {
                p = Runtime.getRuntime().exec("chmod 777 " +  destDir );
                status = p.waitFor();   
                if (status == 0) {    
                    //chmod succeed   
                    Toast.makeText(this, "chmod succeed", Toast.LENGTH_LONG).show();
                } else {    
                    //chmod failed 
                    Toast.makeText(this, "chmod failed", Toast.LENGTH_LONG).show();
                }  
            }

 

友情提醒:

如果是在sdcard下插入,最好先判斷sdcard是否插入,代碼如下//首先判斷sdcard是否插入String status = Environment.getExternalStorageState();
  if (status.equals(Environment.MEDIA_MOUNTED)) {
   return true;
  } else {
   return false;
  } 參考:http://www.devdiv.net/blog/space-28742-do-blog-id-1956.html            http://www.phpfans.net/article/htmls/201009/MzAzNjMz.html
相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.