Android Code Snippet 📌 [B4A] Save a file to the Download folder (and document, pictures, music, movies, dcim folders)

Hello everyone, 👋

With the latest Android restrictions, direct access to the Download folder is no longer possible. However, by using MediaStore, we can save files in a way that’s fully compatible with the new system policies.

🔹 What does this snippet do?
✅ Saves a file directly to the Download folder
✅ Fully compatible with the latest Android versions
✅ Does not require special permissions like WRITE_EXTERNAL_STORAGE
✅
The app can be published on Google Play without special permissions

I’ve created a simple solution in B4A that leverages MediaStore to bypass these restrictions and make saving files to the Download folder easy and safe.

📂 Usage example:
This code demonstrates how to save a file to the Download folder using MediaStore and doesn’t require extra permissions. It works seamlessly with the latest Android security policies.

🔗 You can see the full source code on Linkedin

Hope this helps! 🚀 Feel free to ask if you have any questions or suggestions.
 
Last edited:

Star-Dust

Expert
Licensed User
Longtime User
For many months now I have been sharing many code fragments on linkedin and GitHub along with a series of articles.

This is part of a newsletter LinkedIn that I wanted to present here too. Many of the things you share here I should use "chit chat" because in addition to the code I like to share arguments

Other attempts made here in the past to do something similar have already failed, especially due to pedantic and aggressive comments from my fellow countrymen. Linkedin seems to me a more suitable place for my idea of spreading because it is selected for professionals.

Linkedin is the best place for articles, chat, and newsletters mixed with code. On B4X instead I share some of my libraries that I develop for work
 
Last edited:

Star-Dust

Expert
Licensed User
Longtime User
I answer someone's question if this method works on each version:
  • Until Android 5 (SDK 22) no permit is required, just indicate external memory.
  • From Android 6 (SDK 23) to Android 8 (SDK 28) you had to ask for a runtime permit for access to external memory
  • With Android 9 (SDK 29) you can use this permit in the manifest: SetapplicationionatTribute (Android: Requestlegacyexteralstorage, True)
  • From Android 10+ (SDK 30+) this is the method to be used to access public folders with download, document, pictures, music, movies, dcim
 

AlfaizDev

Well-Known Member
Licensed User
Hello everyone, 👋

With the latest Android restrictions, direct access to the Download folder is no longer possible. However, by using MediaStore, we can save files in a way that’s fully compatible with the new system policies.

🔹 What does this snippet do?
✅ Saves a file directly to the Download folder
✅ Fully compatible with the latest Android versions
✅ Does not require special permissions like WRITE_EXTERNAL_STORAGE
✅
The app can be published on Google Play without special permissions

I’ve created a simple solution in B4A that leverages MediaStore to bypass these restrictions and make saving files to the Download folder easy and safe.

📂 Usage example:
This code demonstrates how to save a file to the Download folder using MediaStore and doesn’t require extra permissions. It works seamlessly with the latest Android security policies.

🔗 You can see the full source code on Linkedin

Hope this helps! Feel free to ask if you have any questions or suggestions.🚀
Cool,
Is this method official or can Google Play suddenly shut it down?
 

jkhazraji

Active Member
Licensed User
Longtime User
is the PERMISSION_WRITE_EXTERNAL_STORAGE requirement related to Android version?
 

Star-Dust

Expert
Licensed User
Longtime User
is the PERMISSION_WRITE_EXTERNAL_STORAGE requirement related to Android version?
I answer someone's question if this method works on each version:
  • Until Android 5 (SDK 22) no permit is required, just indicate external memory.
  • From Android 6 (SDK 23) to Android 8 (SDK 28) you had to ask for a runtime permit for access to external memory
  • With Android 9 (SDK 29) you can use this permit in the manifest: SetapplicationionatTribute (Android: Requestlegacyexteralstorage, True)
  • From Android 10+ (SDK 30+) this is the method to be used to access public folders with download, document, pictures, music, movies, dcim


Post in thread 'permission WRITE_EXTERNAL_STORAGE' https://www.b4x.com/android/forum/threads/permission-write_external_storage.162324/post-995741
 

Star-Dust

Expert
Licensed User
Longtime User

Star-Dust

Expert
Licensed User
Longtime User
The code is the same. You have to copy the SUB into the pages where you want to use it.
 

asales

Expert
Licensed User
Longtime User
Hi @Star-Dust.
I tested the example and the icon file is downloaded and saved in Download folder, but the name of the file is change to "1739408281111.json" (the current time tick with json extension).
Same behavior in real device with Android 10 and emulator with Android 15.
What could be this?
 

Star-Dust

Expert
Licensed User
Longtime User
the extension depends on the mime set, in fact in the second example that you find on linkedin it sets the mime based on the extension. The file name is random chosen by the operating system, it is not possible to intervene on that.
 

Star-Dust

Expert
Licensed User
Longtime User
If I could intervene on the name I would use one of those folders to store a Database and recover it later to update it. I would practically be able to overcome the protections on writing to the external memory. I wish it were possible...
 

Star-Dust

Expert
Licensed User
Longtime User
Hi @Star-Dust.
I tested the example and the icon file is downloaded and saved in Download folder, but the name of the file is change to "1739408281111.json" (the current time tick with json extension).
Same behavior in real device with Android 10 and emulator with Android 15.
What could be this?
I updated the source code to ensure that the file is saved with the original name
 
Top