Android Download Manager Save To /system
I'm just playing around with Android Studio and I'm trying to figure out how to Download a file into /system. I understand I need root for this and I already got that part working,
Solution 1:
What I did is that:
(It's a part in one of my projects)
/*****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();
}
The wait-until-download-complete-thingy is a bit hacky, I found it here. It will maybe not work with files that take more than 5 seconds to download
Post a Comment for "Android Download Manager Save To /system"