【问题标题】:how can access to video path in my device?如何访问我设备中的视频路径?
【发布时间】:2021-10-21 10:28:41
【问题描述】:
 @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()
    }
}

我正在使用 CameraView 录制视频。 当停止记录时,我得到记录路径

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 怎么能访问到这个路径。

如果 Kotlin 有另一种方法可以将视频保存到存储(写入文件的最佳方式)

【问题讨论】:

  • 您使用哪个应用程序在您的应用程序私有目录中查找文件?在 Android 11+ 设备上,文件管理器等其他应用无法访问您应用的私有目录,通常也无法访问 /storage/emulated/0/Android/data。
  • 如果文件管理器应用程序应该能够看到您的文件,则保存到 getExternalStorageDirectory(Environment.DIRECTORY_MOVIES) 或使用 MediaStore.insert() 获取该目录中文件的 uri。

标签: android kotlin camera-view


【解决方案1】:

使用这个:

    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
    }

此代码将视频 uri 插入到 MediaStore,检索其文件路径,并将其从 MediaStore 中删除。此路径将指向公开且不需要权限的 Movies 目录。现在您可以使用此文件路径创建一个File 对象,如下所示: val file = File(createVideoOutputPath(context))

【讨论】:

    猜你喜欢
    • 2017-05-28
    • 1970-01-01
    • 2023-01-07
    • 1970-01-01
    • 2017-02-22
    • 1970-01-01
    • 2017-11-21
    • 2019-10-27
    • 1970-01-01
    相关资源
    最近更新 更多