2016-01-13 63 views
2

你好我一直在尋找方法來刪除或重命名手機內部存儲中的特定文件。具體而言,我的目標是waze文件夾中的文件,位於內部存儲的根文件夾中。正如我所說,我想了解更多關於這方面的信息,但沒有什麼適合我的,所以我認爲我的錯誤可能在我正在使用的路徑中。這裏是我的代碼:如何刪除和重命名Android中的文件?

重命名:

file_Path = "/data/data/waze" 
    File from  = new File(file_Path, "currentFileName"); 
    File to  = new File(file_Path, "newFilename"); 
    from.renameTo(to); //this method returns me False 

刪除:

file_Path ="/data/data/waze/file" 
File file = new File(file_Path); 
boolean deleted = file.delete(); 

我嘗試了很多的方法可以做到這一點,但是這是一個我認爲是附近獲得它。所以如果你們中的任何人都可以指出我的錯誤/我會感謝你!來自哥斯達黎加的擁抱!

回答

7

除了自己的應用程序文件外,您沒有對internal storage文件的讀取或寫入權限。您不能重命名或從其他應用程序中刪除文件,例如Waze。

例外情況是,在有根設備上,您可以要求fork進程具有超級用戶權限,並且這些進程將具有設備範圍的讀/寫訪問權限。

+0

這是非常可悲的jaja!謝謝你的幫助! –

+0

你也可以接受它作爲最好的答案。 – Mohsen

1

爲了完成@CommonsWare的答案,你可以檢查設備是否有根,然後執行方法或別的什麼。

下面是一個例子,

摘自:http://www.stealthcopter.com/blog/2010/01/android-requesting-root-access-in-your-app/

Process p; 
try { 
    // Preform su to get root privledges 
    p = Runtime.getRuntime().exec("su"); 

    // Attempt to write a file to a root-only 
    DataOutputStream os = new DataOutputStream(p.getOutputStream()); 
    os.writeBytes("echo \"Do I have root?\" >/system/sd/temporary.txt\n"); 

    // Close the terminal 
    os.writeBytes("exit\n"); 
    os.flush(); 
    try { 
     p.waitFor(); 
      if (p.exitValue() != 255) { 
       // TODO Code to run on success 
       toastMessage("root"); 
      } 
      else { 
       // TODO Code to run on unsuccessful 
       toastMessage("not root");  
      } 
    } catch (InterruptedException e) { 
     // TODO Code to run in interrupted exception 
     toastMessage("not root"); 
    } 
} catch (IOException e) { 
    // TODO Code to run in input/output exception 
    toastMessage("not root"); 
} 

或者你可以看看:

http://su.chainfire.eu/#how

https://github.com/Chainfire/libsuperuser

而且,使用下面的許可,您的清單太:

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

還是一個很好的例子可以在Github上:

https://github.com/mtsahakis/RootChecker

相關問題