Skip to content Skip to sidebar Skip to footer

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.Requestrequest=newDownloadManager.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*****/DownloadManagermanager= (DownloadManager) getSystemService(Context.DOWNLOAD_SERVICE);
manager.enqueue(request);

Filemvfile=newFile("/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*****/Processsu= Runtime.getRuntime().exec("su");
    DataOutputStreamoutputStream=newDataOutputStream(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"