2015-06-06 84 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进行纠正。

相关问题