How to store files created from the application in the Android Downloads folder?

I create an excelheet in my application, which, when generated, should automatically be saved in the Downloads folder of any Android device, where all downloads are usually saved.

I have the following which saves the file in the My Files folder -

File file = new File(context.getExternalFilesDir(null), fileName); 

as a result -

 W/FileUtils﹕ Writing file/storage/emulated/0/Android/data/com.mobileapp/files/temp.xls 

I rather want to save the generated file automatically in the Downloads folder when creating the excel sheet.

Update # 1: See snapshot here. I want the one that is red and what you suggested is stored in the same blue circle (/ storage / emulated / 0 / download), if that makes sense. Please advise how I can save the file in one red red, that is, the "Downloads" folder, which is different from / storage / emulated / 0 / Download in the "MyFiles" section

snapshot

+14
source share
5 answers

Use this to get the directory:

 Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS); 

And don't forget to set this permission in your manifest.xml:

 <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/> 
+22
source

Now, from your question, edit, I think, better understand what you want. The files displayed in the "Download" menu from one of the red edges are those that are actually downloaded through the DownloadManager , although the previous steps that I provided you will save the files in the download folder, but they will not be displayed in this menu, t Downloaded. However, to complete this work, you must start downloading your file so that it can show here.

Here is an example of how you can start the download:

  DownloadManager.Request request = new DownloadManager.Request(uri); request.setDestinationInExternalPublicDir(Environment.DIRECTORY_DOWNLOADS, "fileName"); request.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED); // to notify when download is complete request.allowScanningByMediaScanner();// if you want to be available from media players DownloadManager manager = (DownloadManager) getSystemService(DOWNLOAD_SERVICE); manager.enqueue(request); 

This method is used to download from Uri, and I have not used it with a local file.

If your file is not from the Internet, you can try to save a temporary copy and get the file Uri for this value.

+12
source

Just use DownloadManager to download the created file as follows:

 File dir = new File("//sdcard//Download//"); File file = new File(dir, fileName); DownloadManager downloadManager = (DownloadManager) context.getSystemService(DOWNLOAD_SERVICE); downloadManager.addCompletedDownload(file.getName(), file.getName(), true, "text/plain",file.getAbsolutePath(),file.length(),true); 

"text/plain" is the type of mime you pass so that it knows which applications can run the downloaded file. It did it for me.

+8
source

I used the following code to make it work in my Xamarin Droid project using C #:

 // Suppose this is your local file var file = new byte[] { 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20 }; var fileName = "myFile.pdf"; // Determine where to save your file var downloadDirectory = Path.Combine(Android.OS.Environment.ExternalStorageDirectory.AbsolutePath, Android.OS.Environment.DirectoryDownloads); var filePath = Path.Combine(downloadDirectory, fileName); // Create and save your file to the Android device var streamWriter = File.Create(filePath); streamWriter.Close(); File.WriteAllBytes(filePath, file); // Notify the user about the completed "download" var downloadManager = DownloadManager.FromContext(Android.App.Application.Context); downloadManager.AddCompletedDownload(fileName, "myDescription", true, "application/pdf", filePath, File.ReadAllBytes(filePath).Length, true); 

Now your local file is “downloaded” to your Android device, the user is notified, and the link to the file is added to the download folder. However, make sure that the user asks for permission before you write to the file system, otherwise the "access denied" exception will be thrown.

+3
source

How Oluwatumbi correctly answered this question, but first you need to check if permission is allowed for WRITE_EXTERNAL_STORAGE following code:

 int MY_PERMISSIONS_REQUEST_WRITE_EXTERNAL_STORAGE = 1; if (ContextCompat.checkSelfPermission(context, Manifest.permission.WRITE_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED) { // Permission is not granted // Request for permission ActivityCompat.requestPermissions(thisActivity, new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE}, MY_PERMISSIONS_REQUEST_WRITE_EXTERNAL_STORAGE); }else{ Uri uri = Uri.parse(yourUrl); DownloadManager.Request request = new DownloadManager.Request(uri); request.setDestinationInExternalPublicDir(Environment.DIRECTORY_DOWNLOADS, "fileName"); request.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED); // to notify when download is complete request.allowScanningByMediaScanner();// if you want to be available from media players DownloadManager manager = (DownloadManager) getSystemService(DOWNLOAD_SERVICE); manager.enqueue(request); } 
+1
source

Source: https://habr.com/ru/post/981638/


All Articles