[英]Android Write file to a different application folder
我试图通过修改一些图像文件来稍微自定义另一个应用程序。 因此,目标是简单地覆盖微小的jpg文件(根据需要一次覆盖一个)
例如,需要覆盖/data/data/com.otherapp/files/somefile.jpg
p = Runtime.getRuntime().exec("su");
在尝试write方法之前已经运行了。
我还添加了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();
}
我怎样才能做到这一点?
好的,它已经花了一整天了,但是我达到了预期的结果:
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();
}
注意在某些情况下,我必须对根文件夹777进行chmod处理才能使其工作。 我确定这不是一个好习惯,这样做将意味着使用该文件夹的应用程序可能无法访问它。 就我而言,就是这样,直到我使用ES Explorer对其进行了纠正
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.