Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android 6.0 - Download file to external storage without asking for permission

I have a superuser access on my device. I used this function very successfully to download and update my application programattically, but since android 6.0 this function stopped working (because of new type of permission requests).

My question is: since I have superuser access on my rooted device, how can edit my function so I can download the external file on the sdcard without asking for permission from user?

here is the function I use to update the app:

public class UpdateAppZ extends AsyncTask<String,Void,Void>{
    private Context context;
    public void setContext(Context contextf){
        context = contextf;
    }

    @Override
    protected Void doInBackground(String... arg0) {
        try {
            URL url = new URL(arg0[0]);
            HttpURLConnection c = (HttpURLConnection) url.openConnection();
            c.setRequestMethod("GET");
            c.setDoOutput(true);
            c.connect();

            String PATH = "/mnt/sdcard/Download/";
            File file = new File(PATH);
            file.mkdirs();
            File outputFile = new File(file, "update.apk");
            if(outputFile.exists()){
                outputFile.delete();
            }
            FileOutputStream fos = new FileOutputStream(outputFile);

            InputStream is = c.getInputStream();

            byte[] buffer = new byte[1024];
            int len1 = 0;
            while ((len1 = is.read(buffer)) != -1) {
                fos.write(buffer, 0, len1);
            }
            fos.close();
            is.close();

            Intent intent = new Intent(Intent.ACTION_VIEW);
            intent.setDataAndType(Uri.fromFile(new File("/mnt/sdcard/Download/update.apk")), "application/vnd.android.package-archive");
            intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); // without this flag android returned a intent error!
            context.startActivity(intent);


        } catch (Exception e) {
            Log.e("UpdateAPP", "Update error! " + e.getMessage());
        }
        return null;
    }
}

and call:

UpdateAppZ atualizaApp = new UpdateAppZ();    
                             atualizaApp.setContext(getApplicationContext()); 
                              atualizaApp.execute("http://85.118.98.251/misho/app-debug.apk");
like image 484
Mister M Avatar asked Oct 19 '25 22:10

Mister M


2 Answers

Download to getExternalFilesDir(). No permission needed while other apps have access.

like image 114
greenapps Avatar answered Oct 22 '25 11:10

greenapps


No you can not do that as from Android 6.0 You need to ask for dangerous permissions at runtime.

Because If the device is running Android 6.0 (API level 23) or higher, and the app's targetSdkVersion is 23 or higher, the OS will enforce the app to request permissions from the user at run-time.

Weather you have root access or not.

Source:
https://developer.android.com/guide/topics/security/permissions.html https://developer.android.com/training/permissions/index.html

like image 26
Shridutt Kothari Avatar answered Oct 22 '25 13:10

Shridutt Kothari



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!