Reputation: 116
@Throws(IOException::class)
private fun createVideoFile(): File {
// Create an image file name
val timeStamp: String = SimpleDateFormat("yyyyMMdd_HHmmss").format(Date())
val storageDir: File? = getExternalFilesDir(Environment.DIRECTORY_MOVIES)
return File.createTempFile(
"oogoo_${timeStamp}_",
".mp4",
storageDir
).apply {
videoPath = this.path
videoUri = this.toUri()
}
}
Iam using CameraView to record Video . when stop record I get path of record
D/VideoTaken: /storage/emulated/0/Android/data/com.example.testgogoapplication/files/Movies/oogoo_20211021_125639_3062139219833544197.mp4
file:///storage/emulated/0/Android/data/com.example.testgogoapplication/files/Movies/oogoo_20211021_125639_3062139219833544197.mp4
put not found this path in Device how can access to this path?.
if there is another way to save the video to storage (best way to write file) by Kotlin
Upvotes: 0
Views: 1675
Reputation: 201
Use this:
private fun createVideoOutputPath(context: Context): String {
val contentResolver = context.contentResolver
/** Represent the videos collection */
val videosCollection: Uri = sdkAndUp(29) { // if sdk is 29 or higher
MediaStore.Video.Media.getContentUri(MediaStore.VOLUME_EXTERNAL_PRIMARY)
} ?: MediaStore.Video.Media.EXTERNAL_CONTENT_URI
/** Represents the data values of the video to be saved */
val contentValues = ContentValues()
// Adding the file title to the content values
contentValues.put(
MediaStore.Video.Media.TITLE,
"VID_" + System.currentTimeMillis() + ".mp4"
)
// Adding the file display name to the content values
contentValues.put(
MediaStore.Video.Media.DISPLAY_NAME,
"VID_" + System.currentTimeMillis() + ".mp4"
)
/** Represents the uri of the inserted video */
val videoUri = contentResolver.insert(videosCollection, contentValues)!!
// Opening a stream on to the content associated with the video content uri
contentResolver.openOutputStream(videoUri)
/** Represents the file path of the video uri */
val outputPath = getUriRealPath(context, videoUri)!!
// Deleting the video uri to create it later with the actual video
contentResolver.delete(videoUri, null, null)
return outputPath
}
private fun getUriRealPath(contentResolver: ContentResolver, uri: Uri): String {
var filePath = ""
val cursor = contentResolver.query(uri, null, null, null, null)
if (cursor != null) {
if (cursor.moveToFirst()) {
var columnName = MediaStore.Images.Media.DATA
when (uri) {
MediaStore.Images.Media.EXTERNAL_CONTENT_URI -> {
columnName = MediaStore.Images.Media.DATA
}
MediaStore.Video.Media.EXTERNAL_CONTENT_URI -> {
columnName = MediaStore.Video.Media.DATA
}
}
val filePathColumnIndex = cursor.getColumnIndex(columnName)
filePath = cursor.getString(filePathColumnIndex)
}
cursor.close()
}
return filePath
}
This code inserts a video uri to MediaStore
, retrieves its file path, and removes it from MediaStore
. This path will be to the Movies directory which is public and does not require permissions. Now you can use this file path to create a File
object like so:
val file = File(createVideoOutputPath(context))
Upvotes: 0