Android Download Manager保存到/ system

时间:2016-02-04 09:49:43

标签: java android android-download-manager

我正在玩Android Studio,我正试图弄清楚如何将文件下载到/ system。

我知道我需要root用户,我已经让那部分工作了,我唯一遇到的麻烦就是

request.setDestinationInExternalPublicDir("/system/", "test.jpg");

目标是下载文件并将其保存到/ system,文件名为test.jpg。

所以最后是文件位于/system/test.jpg。

这个问题是DownloadManager将其保存到内部存储,并创建一个名为'system'的新文件夹。

我可以告诉它与setDestinationInExternalPublicDir有关,但我只是不确定要将其更改为什么。

再次感谢

1 个答案:

答案 0 :(得分:0)

我所做的是:
(它是我项目中的一部分)

/*****DOWNLOAD FILE*****/
DownloadManager.Request request = new DownloadManager.Request(Uri.parse("https://github.com/pelya/android-keyboard-gadget/blob/master/hid-gadget-test/hid-gadget-test?raw=true"));
request.setDescription("hid-gadget-test");
request.setTitle("hid-gadget-test");
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
    request.allowScanningByMediaScanner();
    request.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE);
}
request.setDestinationInExternalPublicDir(Environment.DIRECTORY_DOWNLOADS, "hid-gadget-test"); /*****SAVE TO DOWNLOAD FOLDER*****/


DownloadManager manager = (DownloadManager) getSystemService(Context.DOWNLOAD_SERVICE);
manager.enqueue(request);

File mvfile = new File("/sdcard/"+Environment.DIRECTORY_DOWNLOADS+"/hid-gadget-test");
while (!mvfile.exists()) {}  /*****WAIT UNTIL DOWNLOAD COMPLETE*****/
try {
    Thread.sleep(5000);
} catch (InterruptedException ignored) {}


try {  /*****RUN MV-COMMAND TO MOVE TO ROOT DIR*****/
    Process su = Runtime.getRuntime().exec("su");
    DataOutputStream outputStream = new DataOutputStream(su.getOutputStream());

    outputStream.writeBytes("mv /sdcard/"+Environment.DIRECTORY_DOWNLOADS+"/hid-gadget-test /data/local/tmp/hid-gadget-test\n");
    outputStream.flush();

    outputStream.writeBytes("exit\n");
    outputStream.flush();
    su.waitFor();
} catch (IOException e) {
    Toast.makeText(getApplicationContext(), "IOException", Toast.LENGTH_SHORT).show();
} catch (InterruptedException e) {
    Toast.makeText(getApplicationContext(), "InterruptedException", Toast.LENGTH_SHORT).show();
}

wait-until-download-complete -thingy有点hacky,我发现它here。它可能不适用于下载时间超过5秒的文件