fun createURIpath(context: Context): Uri {
val fileName = SimpleDateFormat("yyyyMMdd_HHmmss", Locale.GERMAN).format(Date())
val path = getExternalFilesDir(DIRECTORY_PICTURES)
val file = File(path, "$fileName.jpg")
return FileProvider.getUriForFile( context, "${BuildConfig.APPLICATION_ID}.fileprovider", file)
}
Note 1
val
fileName = SimpleDateFormat("yyyyMMdd_HHmmss", Locale.GERMAN).format(Date())
The file name I do as the date and time, for example:
3 january 2024 19:38:37
val
fileName =
"20240103_193837.jpg"
Note 2
val
path =
getExternalFilesDir(
DIRECTORY_PICTURES)
DIRECTORY_PICTURES This is the
Android system constant, there is more:
DIRECTORY_MUSIC,
DIRECTORY_MOVIES,
DIRECTORY_DOCUMENTS
getExternalFilesDir is a
Android system function that returns the full path to the directory where our application can place persistent files
path in our case, this is the folder where the images will be saved, because we use
DIRECTORY_PICTURES
path =
"/storage/emulated/0/Android/data/com.example.androidkotlinapp1/files/Pictures"
Read more:
What is getExternalFilesDir in Android Studio, Kotlin ...
Note 3
val
file = File(path, "$fileName.jpg")
Create an empty
jpg file in the
path folder named
fileName
On a note 4
return
FileProvider.getUriForFile( context, "${BuildConfig.APPLICATION_ID}.fileprovider", file)
FileProvider.getUriForFile is a system function that uses a file and
application id to return the
uri path.
The
uri path will return:
content://com.example.androidkotlinapp1.fileprovider/pictures/20240103_191203.jpg