temp_
temp_

Reputation: 1317

Android: Download Manager Request to Cache directory

I need to save sensitive files to a cached directory using the native Download Manager. A location that can not be discovered by the user. I can easily download to the user's external file system using DownloadManager.Request(). Although using setDestinationInExternalPublicDir(), or any other way to set destination, does not allow me to save to a cached directory. Am I missing something here?

Upvotes: 3

Views: 3359

Answers (2)

Christos Themelis
Christos Themelis

Reputation: 189

You have to forget the Android build in Download manager and create your own manager. Then you can download to path: getCacheDir().getAbsolutePath();

Here is a sample code to download a file yourself without build-in manager

public String downloadFile(String fileURL, String fileName) {

        StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
        StrictMode.setThreadPolicy(policy);

        Log.d(TAG, "Downloading...");
        try {
            int lastDotPosition = fileName.lastIndexOf('/');
            if( lastDotPosition > 0 ) {
                String folder = fileName.substring(0, lastDotPosition);
                File fDir = new File(folder);
                fDir.mkdirs();
            }

            //Log.i(TAG, "URL: " + fileURL);
            //Log.i(TAG, "File: " + fileName);
            URL u = new URL(fileURL);
            HttpURLConnection c = (HttpURLConnection) u.openConnection();
            c.setRequestMethod("GET");
            c.setReadTimeout(30000);
            c.connect();
            double fileSize  = (double) c.getContentLength();
            int counter = 0;
            while ( (fileSize == -1) && (counter <=30)){
                c.disconnect();
                u = new URL(fileURL);
                c = (HttpURLConnection) u.openConnection();
                c.setRequestMethod("GET");
                c.setReadTimeout(30000);
                c.connect();
                fileSize  = (double) c.getContentLength();
                counter++;
            }

            File fOutput = new File(fileName);
            if (fOutput.exists())
                fOutput.delete();

            BufferedOutputStream f = new BufferedOutputStream(new FileOutputStream(fOutput));
            InputStream in = c.getInputStream();
            byte[] buffer = new byte[8192];
            int len1 = 0;
            int downloadedData = 0;
            while ((len1 = in.read(buffer)) > 0) {
                downloadedData += len1;
                f.write(buffer, 0, len1);
            }
            Log.d(TAG, "Finished");
            f.close();

            return fileName;
        }
        catch (Exception e) {
            e.printStackTrace();
            Log.e(TAG, e.toString());
            return null;
        }
    }

Upvotes: 2

Rutger
Rutger

Reputation: 134

The build in DownloadManager can not save files to internal directories. Only to external directories like the SD card and other public directories e.g. the video or photos folder.

Upvotes: 1

Related Questions