version 1.0
IntroductionHave you ever wondered how you can save a file in the Documents or the Downloads folder?
After a brainstorming session I had with [B]NJDude[/B] (was actually his idea), the PublicFile class was born and will assist you with this task from now on.
Latest package (library files and samples)
https://www.dropbox.com/s/plxiq0bai4f3y0h/PublicFile.zip
Precompiled sample APK
https://www.dropbox.com/s/mxbz37ks35zap6j/sample.apk
How to use
IMPORTANT: IT DOES NOT WORK IN THE EMULATOR. USE A REAL DEVICE!
Add this to your manifest file:
B4X:
AddPermission("android.permission.WRITE_EXTERNAL_STORAGE")
Now, the only thing you need is the PublicFile.Save method.
Choose the desired location+name, pass an InputStream to it and you are done!
B4X:
Dim MyFile as PublicFile = MyFile.Save(MyFile.DirDownloads, "file.zip", Input)
Where Input is an InputStream containing the data to be saved.
Examples:
Saving a text file:
B4X:
'The text to be written
Dim Text As String = "b4a-rocks!" 'Yeap, it does :D
'Create an InputStream and add the text to it
Dim Input As InputStream
Dim Data() As Byte = Text.GetBytes("UTF8")
Input.InitializeFromBytesArray(Data, 0, Data.Length)
'Create a PublicFile and pass the InputStream
Dim MyFile As PublicFile
MyFile.Save(MyFile.DirDocuments, "publicfile.txt", Input)
Saving an image file:
B4X:
'Open the picture stream
Dim Input As InputStream
Input = File.OpenInput(File.DirAssets, "image.jpg")
'Create a PublicFile and pass the picture stream
Dim MyFile As PublicFile
MyFile.Save(MyFile.DirPictures, "publicfile.jpg", Input)
Available PublicFile.Dir(...) constants:
- DirMovies
- DirPictures
- DirDocuments
- DirDCIM
- DirAlarms
- DirMusic
- DirNotifications
- DirDownloads
- DirPodcasts
- DirRingtones
Don't panic tips:
- In some devices, some folder locations may not be available.
- The Gallery, is a virtual folder that is updated whenever the system has indexed your file. That means that your saved picture will appear few moments later.
Version history:
1.0
- Initial version
--
That's all for now folks!
Last edited: