2015-06-06 86 views
0
  • 我的設備是植根
  • 目標受衆(如應用程序共享)將植根設備只

我試圖通過自定義其他應用程序稍微修改一些圖像文件。因此,目標是(每次一個根據需要)來簡單地覆蓋微小的jpg文件需要Android的寫文件到不同的應用程序文件夾

/data/data/com.otherapp/files/somefile.jpg例如被覆蓋

p = Runtime.getRuntime().exec("su");已經運行之前,我試圖寫方法。

上類似的問題

我得到許可被拒絕的建議我也寫代碼之前添加Runtime.getRuntime().exec("chmod 077 " +myFile.getAbsolutePath());java.io.FileNotFoundException: /data/data/com......jpg: open failed: EACCES (Permission denied)

寫代碼,我已經是:

//myDir is /data/data/com....... 
    File myFile = new File (myDir.getAbsolutePath(), fname); 
    try { 
     //myFile.createNewFile();//tried with and without this 
     FileOutputStream out = new FileOutputStream(myFile); 
     btbmp.compress(Bitmap.CompressFormat.JPEG, 60, out); 
     out.flush(); 
     out.close(); 
    } catch (Exception e) { 
      e.printStackTrace(); 
    } 

我怎樣才能做到這一點?

+0

寫入SD卡的相同任務完全沒有問題。 '使用權限等所有設置正確' – Onimusha

+0

從我所知,一個應用程序無法訪問其他的應用程序私人文件(例如在安裝目錄) –

+0

@SkarosIlias是通常不可能,但與根可以作爲其他答案已經表明這但沒有任何信息'如何' – Onimusha

回答

2

好它採取了一整天,但我已經達到這樣理想的結果:

  • 創建SD卡上的文件
  • 然後文件複製到根目的地

src_file = "somefile/on/sdcard.jpg"

dest_file = "/data/data/com.anotherapp/files/abcde.jpg"使用上下文獲得路徑

try { 
     Process process = Runtime.getRuntime().exec("su"); 

     DataOutputStream os = new DataOutputStream(process.getOutputStream()); 
     os.writeBytes("rm "+dest_file + "\n"); //removes destination file -might not be required - haven't tested 
     os.flush(); 
     os.writeBytes("cat "+src_file+" > "+dest_file + "\n"); 
     os.flush(); 
     os.writeBytes("exit\n"); 
     os.flush(); 

     // Waits for the command to finish. 
     process.waitFor(); 
    } 
    catch (IOException e) { 
     e.printStackTrace(); 
    } 
    catch (InterruptedException e) { 
     e.printStackTrace(); 
    } 

注意在某些情況下,我必須chmod根文件夾777才能正常工作。我相信這不是一個好的做法,這樣做意味着使用該文件夾的應用程序可能無法訪問它。在我的情況下,這是發生了什麼,直到我使用ES Explorer進行糾正。

相關問題