Lets say I\'m developing a chat app that is able to share with others ANY kind of files (no mimetype restriction): like images, videos, documents, but also compressed files like
You can use the Android DownloadManager.Request
.
It will need the WRITE_EXTERNAL_STORAGE
Persmission until Android 9. From Android 10/ Q and above it will not need any permission (it seems it handle the permission itself).
If you want to open the file afterwards, you will need the user's permission instead (also if you only want to open it within an external app (e.g. PDF-Reader).
You can use the download manager like this:
DownloadManager.Request request = new DownloadManager.Request();
request.addRequestHeader("Accept", "application/pdf");
// Save the file in the "Downloads" folder of SDCARD
request.setDestinationInExternalPublicDir(Environment.DIRECTORY_DOWNLOADS,filename);
DownloadManager downloadManager = (DownloadManager) getActivity().getSystemService(Context.DOWNLOAD_SERVICE);
downloadManager.enqueue(request);
This are the references: https://developer.android.com/reference/kotlin/android/app/DownloadManager.Request?hl=en#setDestinationInExternalPublicDir(kotlin.String,%20kotlin.String)
https://developer.android.com/training/data-storage