保存的视频不会显示在图库中

aya*_*-ya 1 android

我希望有人可以帮我解决这个问题.

我有一个应用程序,使用户可以拍摄视频(.mp4)或选择现有的视频.如果拍摄视频,视频会保存到公共目录,我会触发Android扫描文件以将其添加到媒体库.

问题是,视频运行正常,我可以在我的应用程序中预览完成的视频.但是同样的视频不会出现在媒体库中,也不会被其他应用程序访问 - 除了FileManager-,即使同一文件夹中的其他.mp4出现在列表中也不错.

更多信息:

  • 在FileManager应用程序中,不显示的文件具有图标视频图标,而显示的文件具有缩略图.我可以通过在FileManager应用程序中剪切和粘贴文件来触发这些未出现的文件添加到媒体库应用程序(所以我认为不是由于文件被破坏).
  • 扫描代码适用于我从现有相机应用程序拍摄图像的代码,它只适用于视频应用程序...

是否需要额外的许可才能使用?我添加/询问/请求从ext写入和读取的权限.存储和相机在我的清单和代码中.

以下是我拍摄视频并将其扫描到图库的方式:

private void takeVideo() {
    Intent takeVideoIntent = new Intent(MediaStore.ACTION_VIDEO_CAPTURE);

    if (takeVideoIntent.resolveActivity(ctx.getPackageManager()) != null) {

        // Create the file where photo should go
        File mediaFile = null;

        try {
            mediaFile = getOutputMediaFile(ctx, MEDIA_TYPE_VIDEO);

        } catch (IOException ex) {
            Log.e("FragCamera", "takeVideo() : Error occurred while creating File." + ex);
        }

        if (mediaFile != null) {
            Uri mediaUri = Uri.fromFile(mediaFile);

            Log.d("FragCamera", "takeVideo() mediaUri: " + mediaUri);

            currMediaUri = mediaUri;
            currPhotoPath = mediaFile.getAbsolutePath();
            Log.d("FragCamera", "takeVideo() currPhotoPath: " + currPhotoPath);

            //make the new file available for other apps
            updateMediaGallery(mediaFile);

            MediaScannerConnection.scanFile(
                    ctx,
                    new String[]{currPhotoPath},
                    new String[]{"video/mp4"},
                    new MediaScannerConnection.OnScanCompletedListener() {
                        @Override
                        public void onScanCompleted(String path, Uri uri) {
                            Log.v("FragCameraScan",
                                    "file " + path + " was scanned seccessfully: " + uri);
                        }
                    });

            takeVideoIntent.putExtra(android.provider.MediaStore.EXTRA_OUTPUT, mediaUri);
            this.startActivityForResult(takeVideoIntent, I_REQUEST_VIDEO_CAPTURE);
        }

    }
}

private void galleryAddPic(String filePath) {
    Intent mediaScanIntent = new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE);
    File f = new File(filePath);
    Uri contentUri = Uri.fromFile(f);
    mediaScanIntent.setData(contentUri);
    ctx.sendBroadcast(mediaScanIntent);
}
Run Code Online (Sandbox Code Playgroud)

每个Logcat值登录上面的代码:

 D/FragCamera: takeVideo() mediaUri: file:///storage/emulated/0/DCIM/Camera/VID_20161207_142021.mp4
 D/FragCamera: takeVideo() currPhotoPath: /storage/emulated/0/DCIM/Camera/VID_20161207_142021.mp4
 V/FragCameraScan: file /storage/emulated/0/DCIM/Camera/VID_20161207_142021.mp4 was scanned seccessfully: null
Run Code Online (Sandbox Code Playgroud)

小智 7

尝试使用此功能

public Uri addVideo(File videoFile) {
    ContentValues values = new ContentValues(3);
    values.put(MediaStore.Video.Media.TITLE, "My video title");
    values.put(MediaStore.Video.Media.MIME_TYPE, "video/mp4");
    values.put(MediaStore.Video.Media.DATA, videoFile.getAbsolutePath());
    return getContentResolver().insert(MediaStore.Video.Media.EXTERNAL_CONTENT_URI, values);
}
Run Code Online (Sandbox Code Playgroud)

'values'只是关于视频的元数据