Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/184.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/video/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/logging/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在使用FileProvider和MediaScan的Android 24上,视频不会显示在照片中_Android_Video_Mediastore - Fatal编程技术网

在使用FileProvider和MediaScan的Android 24上,视频不会显示在照片中

在使用FileProvider和MediaScan的Android 24上,视频不会显示在照片中,android,video,mediastore,Android,Video,Mediastore,我有点被困在这一点上,已经工作了一段时间,没有运气。我有一个android应用程序,允许用户拍照/视频。这些文件使用文件提供程序存储在外部存储器上。我一直在尝试(运气不佳)让视频显示在Photos应用程序中,但一直未能实现。Intent.ACTION\u MEDIA\u SCANNER\u SCAN\u文件和sendBroadcast非常适合图像,但不适用于视频。我知道视频正在被捕获,它们被保存在正确的位置,并且没有损坏(我可以使用FileProvider内容Uri在VideoView中播放它们

我有点被困在这一点上,已经工作了一段时间,没有运气。我有一个android应用程序,允许用户拍照/视频。这些文件使用文件提供程序存储在外部存储器上。我一直在尝试(运气不佳)让视频显示在Photos应用程序中,但一直未能实现。Intent.ACTION\u MEDIA\u SCANNER\u SCAN\u文件和sendBroadcast非常适合图像,但不适用于视频。我知道视频正在被捕获,它们被保存在正确的位置,并且没有损坏(我可以使用FileProvider内容Uri在VideoView中播放它们)我还没有尝试将其更新到Android 10,因为我的最低目标是24,我似乎无法使其工作

创建Intent.ACTION\u MEDIA\u SCANNER\u SCAN\u文件并发送广播视频不会显示。如果我使用MediaScannerConnection,它不会显示。如果我使用内容URI手动插入内容解析器,它会显示在照片中,但视频无法播放,并且没有预览。如果我用绝对路径手动插入内容解析器,它根本不会显示在照片中 下面是我用来拍摄视频并添加到MediaStore的代码

文件提供者

    <provider
        android:name="androidx.core.content.FileProvider"
        android:authorities="${applicationId}.FileProvider"
        android:exported="false"
        android:grantUriPermissions="true">
        <meta-data
            android:name="android.support.FILE_PROVIDER_PATHS"
            android:resource="@xml/file_paths" />
    </provider>
OnActivityResult代码我已经尝试过了,但都没有成功。我尝试了内容URI和绝对路径,但没有成功

重写activityResult(请求代码:Int,结果代码:Int,数据:Intent?){ super.onActivityResult(请求代码、结果代码、数据)

我已经看过了很多关于堆栈的Anwser,但没有任何效果。非常感谢您的帮助


发布更新。我通过创建视频而不提供文件路径来解决此问题。这会在DCIM文件夹中创建视频。创建视频时,我会保存uri,并查询uri以确保它仍然存在于应用程序的负载中。这与我想要的不完全一样,但它可以工作。

发布更新。我通过创建在不提供文件路径的情况下复制视频。这会在DCIM文件夹中创建视频。创建视频时,我会保存uri,并查询uri以确保它仍然存在于应用程序的负载中。这与我想要的不完全一样,但它会起作用

<?xml version="1.0" encoding="utf-8"?>
<paths xmlns:android="http://schemas.android.com/apk/res/android">
    <external-files-path name="Image" path="Image" />
    <external-files-path name="Recording" path="Recording"/>
    <external-files-path name="Video" path="Video"/>
</paths>
private fun takeVideo() {

        Intent(MediaStore.ACTION_VIDEO_CAPTURE).also{ videoIntent ->
            val videoFile: File? = try{
                createVideoFile()
            }catch(ex: IOException){
                Toast.makeText(mMainActivityHelper.contextProvider(), R.string.error_taking_photo, Toast.LENGTH_SHORT).show()
                Log.w("OPENING_VIDEO_FILE", "Error opening video file with message = " + ex.message)
                return
            }
            mFilePath = Uri.parse(videoFile?.absolutePath)
            videoFile?.also{
                videoIntent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION)
                mFileUri = FileProvider.getUriForFile(mMainActivityHelper.contextProvider(), "FileProvider", it)
                videoIntent.putExtra(MediaStore.EXTRA_OUTPUT, mFileUri)
           mMainActivityHelper.startActivityForResult(videoIntent, MainActivity.TAKE_VIDEO)
       }
   }
}



@Throws(IOException::class)
private fun createVideoFile(): File? {
    val timeStamp: String = SimpleDateFormat("yyyyMMdd_HHmmss", Locale.getDefault()).format(Date())
    val externalDir = mMainActivityHelper.externalFilesDirProvider(null)
    val imagePath = File(externalDir, "NuNoteVideo")

    if(!imagePath.exists())
        imagePath.mkdir()

    return File(imagePath.path, "MPEG_${timeStamp}" + ".mp4")
}
    when{
          requestCode == TAKE_VIDEO && resultCode == Activity.RESULT_OK->{

            val cv = ContentValues(3).apply {
                val absolutePath = myObjectWithThePath.mFilePath.toString()
                val name = absolutePath.substring(absolutePath.lastIndexOf("/") + 1)
                put(MediaStore.Video.Media.TITLE, name)
                put(MediaStore.Video.Media.DISPLAY_NAME, name)
                put(MediaStore.Video.Media.MIME_TYPE, "video/mp4")
                put(MediaStore.Video.Media.DATE_ADDED, Date().time)
                put(MediaStore.MediaColumns.DATA, absolutePath)
                contentResolver.insert(MediaStore.Video.Media.EXTERNAL_CONTENT_URI, this)
            }

            Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE).also { mediaScanIntent ->
                val fileUri = myObjectWithThePath.mFileUri

                if(fileUri != null) {
                    val f = File(fileUri)
                    mediaScanIntent.data = Uri.fromFile(f)
                    //mediaScanIntent.type = "video/mp4"
                    sendBroadcast(mediaScanIntent)
                }
            }

            val f = File(myObjectWithThePath.mFilePath.toString())

            MediaScannerConnection.scanFile(applicationContext, listOf(f.absolutePath).toTypedArray(), listOf("video/mp4").toTypedArray()){
                path, uri ->

                print(path)
                print(uri) //This shows a URI if i use the absolute path but nothing shows up in the photos app. If i use content uri with FileProvider is null
            }