Check the request.setDestination functions here To Store file in External App-Specific Directory [example: "external/Android/data/your_app_name/filePath_you_set_in_function”], use like below:

DownloadManager.Request request = new DownloadManager.Request(uri);
request.setDescription("Selected Video is being downloaded");
request.allowScanningByMediaScanner();
request.setTitle("Downloading Video");
request.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED);
//Set the local destination for the downloaded file to a path within the application's external files directory
request.setDestinationInExternalFilesDir(context, Environment.DIRECTORY_DOWNLOADS, fileName); //To Store file in External Public Directory use "setDestinationInExternalPublicDir(Environment.DIRECTORY_DOWNLOADS, fileName)"
DownloadManager manager = (DownloadManager)
mContext.getSystemService(Context.DOWNLOAD_SERVICE);
manager.enqueue(request);

And if you want to download it to another place you need to move it after your download gets finished, by using IO streams. You can use a broadcast receiver to perform this task once the DownloadManager has finished downloading. You can use FileProvider to open the file with another app in Android version 10 or above.

Answer from Akki on Stack Overflow
🌐
Talkerscode
talkerscode.com › howto › android-download-file-from-url-and-save-to-internal-storage.php
Android Download File From URL And Save To Internal Storage
Name your new project InternalStorageDemo when you create it. To add the following code, open res -> layout -> activity main.xml (or) main.xml. In this step, we enter MainActivity and add the save and next functions that were defined over the button onclick. The save function takes the information from edittext and stores it within a file in byte format.
Top answer
1 of 2
4

Check the request.setDestination functions here To Store file in External App-Specific Directory [example: "external/Android/data/your_app_name/filePath_you_set_in_function”], use like below:

DownloadManager.Request request = new DownloadManager.Request(uri);
request.setDescription("Selected Video is being downloaded");
request.allowScanningByMediaScanner();
request.setTitle("Downloading Video");
request.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED);
//Set the local destination for the downloaded file to a path within the application's external files directory
request.setDestinationInExternalFilesDir(context, Environment.DIRECTORY_DOWNLOADS, fileName); //To Store file in External Public Directory use "setDestinationInExternalPublicDir(Environment.DIRECTORY_DOWNLOADS, fileName)"
DownloadManager manager = (DownloadManager)
mContext.getSystemService(Context.DOWNLOAD_SERVICE);
manager.enqueue(request);

And if you want to download it to another place you need to move it after your download gets finished, by using IO streams. You can use a broadcast receiver to perform this task once the DownloadManager has finished downloading. You can use FileProvider to open the file with another app in Android version 10 or above.

2 of 2
2

simply add this line to save file:

request.setDestinationInExternalFilesDir(context,Environment.DIRECTORY_ALARM,mp3name);  

to delete this file

File file = new 
File(context.getExternalFilesDir(Environment.DIRECTORY_ALARMS),"Fav_Ringtone.mp3");

file.delete();

to read this file

File file= newFile(context.getExternalFilesDir(Environment.DIRECTORY_ALARMS),"Fav_Ringtone.mp3");
🌐
Medium
medium.com › @zohaibansari100 › a-guide-for-downloading-files-to-external-storage-in-your-android-application-e4b2d8193d41
A guide for Downloading files to External Storage through your Android Application. | by Zed | Medium
November 21, 2019 - After searching through many recent open source projects and asking devs on stackoverflow and reddit, I realized that everyone was achieving this in a similar way, i.e using a separate file which manages the download by creating a File instance programmatically and passing it to the local storage through a file output stream. Below I am going to show you how you can do that as well, I am going to take the example of downloading an image whose URL I receive through an API call, the project is entirely in Kotlin and I am going to be using the image caching library Glide for receiving the image as a bitmap from the URL.
🌐
Stack Overflow
stackoverflow.com › questions › 35531736 › download-image-to-internal-storage
android - Download image to internal storage - Stack Overflow
Try doing this tmpFile = new File(context.getFilesDir(), "news.png"); or see this documentation and according to documentation getCacheDir returns internal storage... for information on how to save files in internal storage see this tutorial link
🌐
Stack Overflow
stackoverflow.com › questions › 58461651 › download-image-using-url-and-save-to-internal-storage
android - Download image using url and save to internal storage - Stack Overflow
{ val url = params[0] val requestOptions = RequestOptions().override(100) .downsample(DownsampleStrategy.CENTER_INSIDE) .skipMemoryCache(true) .diskCacheStrategy(DiskCacheStrategy.NONE) mContext.get()?.let { val bitmap = Glide.with(it) .asBitmap() .load(url) .apply(requestOptions) .submit() .get() try { val path = File(Environment.DIRECTORY_DCIM) val dir =path.absolutePath val file=File(dir+"/EAcademy","schoolImage.jpg") if (!file.exists()) { file.mkdir() } val out = FileOutputStream(file) bitmap.compress(Bitmap.CompressFormat.JPEG, 85, out) out.flush() out.close() Log.i("Tribhuwan", "Saved Image") } catch (e: Exception) { Log.i("Tribhuwan", "Failed to save") } } } }
Find elsewhere
🌐
Android Dvlpr
androiddvlpr.com › home › android download image from url and requesting permissions
Android Download Image from URL and Requesting Permissions
March 13, 2019 - Android Download Image from URL and request additional permissions to save the downloaded iamge in Internal storage of user's phone memory.
Top answer
1 of 2
7

I can not believe i solved this. What I do is replacing:

getFilesDir()

to

Environment.getExternalStorageDirectory()

below I post my final code

    class DownloadFileFromURL extends AsyncTask<String, String, String> {
    ProgressDialog pd;
    String pathFolder = "";
    String pathFile = "";

    @Override
    protected void onPreExecute() {
        super.onPreExecute();
        pd = new ProgressDialog(DashboardActivity.this);
        pd.setTitle("Processing...");
        pd.setMessage("Please wait.");
        pd.setMax(100);
        pd.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
        pd.setCancelable(true);
        pd.show();
    }

    @Override
    protected String doInBackground(String... f_url) {
        int count;

        try {
            pathFolder = Environment.getExternalStorageDirectory() + "/YourAppDataFolder";
            pathFile = pathFolder + "/yourappname.apk";
            File futureStudioIconFile = new File(pathFolder);
            if(!futureStudioIconFile.exists()){
                futureStudioIconFile.mkdirs();
            }

            URL url = new URL(f_url[0]);
            URLConnection connection = url.openConnection();
            connection.connect();

            // this will be useful so that you can show a tipical 0-100%
            // progress bar
            int lengthOfFile = connection.getContentLength();

            // download the file
            InputStream input = new BufferedInputStream(url.openStream());
            FileOutputStream output = new FileOutputStream(pathFile);

            byte data[] = new byte[1024]; //anybody know what 1024 means ?
            long total = 0;
            while ((count = input.read(data)) != -1) {
                total += count;
                // publishing the progress....
                // After this onProgressUpdate will be called
                publishProgress("" + (int) ((total * 100) / lengthOfFile));

                // writing data to file
                output.write(data, 0, count);
            }

            // flushing output
            output.flush();

            // closing streams
            output.close();
            input.close();


        } catch (Exception e) {
            Log.e("Error: ", e.getMessage());
        }

        return pathFile;
    }

    protected void onProgressUpdate(String... progress) {
        // setting progress percentage
        pd.setProgress(Integer.parseInt(progress[0]));
    }

    @Override
    protected void onPostExecute(String file_url) {
        if (pd!=null) {
            pd.dismiss();
        }
        StrictMode.VmPolicy.Builder builder = new StrictMode.VmPolicy.Builder();
        StrictMode.setVmPolicy(builder.build());
        Intent i = new Intent(Intent.ACTION_VIEW);

        i.setDataAndType(Uri.fromFile(new File(file_url)), "application/vnd.android.package-archive" );
        i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);

        getApplicationContext().startActivity(i);
    }

}

Simply put this code to use this class

new DownloadFileFromURL().execute("http://www.yourwebsite.com/download/yourfile.apk");

This code can perform file download to your internal phone storage with a progress bar and continue asking your permission for application install.

Enjoy it.

2 of 2
0

as we know getFilesDir() returns the absolute path to the directory on the filesystem where files created, which will give you path /data/data/your package/files

so you can find the file(if downloaded completely) there

i suggest you to read this article:

How to get the each directory path

🌐
Stack Overflow
stackoverflow.com › questions › 44867730 › how-to-save-a-downloaded-file-into-internal-storage-in-android
download - how to save a downloaded file into internal storage in android? - Stack Overflow
July 2, 2017 - i have used the below code to download a video from server in android studio and it works correctly , but when i search the video in my device i cant find it anywhere ... where does it save and how can i change destination directory on "internal storage"? private DownloadManager downloadManager; btn_download_video.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { downloadManager = (DownloadManager) getSystemService(Context.DOWNLOAD_SERVICE); Uri uri = Uri.parse(urlVideo); DownloadManager.Request request = new DownloadManager.Request(uri); request.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED); request.setDestinationInExternalFilesDir(Video_detail_Activity.this, Environment.DIRECTORY_DOWNLOADS, videoName); Long reference = downloadManager.enqueue(request); } });
🌐
GitHub
gist.github.com › dynoChris › 1591c6a7c6a70c6edc0a642c29ea505a
How to download file to Internal Storage in Android · GitHub
Clone via HTTPS Clone using the web URL. ... Clone this repository at &lt;script src=&quot;https://gist.github.com/dynoChris/1591c6a7c6a70c6edc0a642c29ea505a.js&quot;&gt;&lt;/script&gt; Save dynoChris/1591c6a7c6a70c6edc0a642c29ea505a to your ...
🌐
Medium
medium.com › @choirulihwan › download-image-and-save-to-storage-in-android-a5890df26196
Download Image and Save to Storage in Android | by Choirul Ihwan | Medium
August 13, 2019 - The next thing to do is create ... EditText along with Button and ImageView. User input the url of image in EditText and press button to download and save it to download folder....
🌐
Stack Overflow
stackoverflow.com › questions › 8570509 › how-to-download-store-files-in-android-in-internal-storage-and-access-it
How to download & store files in android in internal storage and access it? - Stack Overflow
I would like to download files from the web to the internal storage of the android device so that it will not be available for the user manipulation and how to access the stored file.For instance a...
🌐
Medium
medium.com › @codeplayon › android-download-video-from-url-and-save-to-internal-storage-dfd0b67fa034
Android download video from URL and save to internal storage | by Codeplayon | Medium
December 29, 2023 - Hi Everyone in this Android Tutorial, I am sharing How to download a video in Android from URL and save it to internal storage. here I am using a string video URL and download the video for these URLs. I am using AsyncTask for downloading video and saving in the phone memory.
🌐
Firebase
firebase.google.com › documentation › cloud storage for firebase › download files with cloud storage on android
Download files with Cloud Storage on Android | Cloud Storage for Firebase
A guide to downloading files from Cloud Storage in your Android app, with options to download in memory, to a local file, or by generating a download URL.