如何在 android marshmallow 中获取创建的视频文件内容

How to fetch the created video file content in android marshmallow

我正在通过 ffmpeg 开发视频编辑应用程序。我能够在除 Marshmallow 之外的所有 android 版本中获取视频内容,例如 ID、DATA、DISPLAY_NAME、大小和持续时间。我在运行时也给了读写权限。

下面是我用来通过 ContentResolver 获取视频内容的代码。

非常感谢任何帮助。

 public static VideoModelLocal getVideoModelFromPath(Context context, String filepath) {
    VideoModelLocal model = null;
    Uri uri = MediaStore.Video.Media.EXTERNAL_CONTENT_URI;
    String[] projection = {
            MediaStore.Video.VideoColumns._ID,
            MediaStore.Video.VideoColumns.DATA,
            MediaStore.Video.VideoColumns.DISPLAY_NAME,
            MediaStore.Video.VideoColumns.DURATION,
            MediaStore.Video.VideoColumns.SIZE,
    };
    String[] selectionArgs = {filepath};
    String selection = MediaStore.Video.VideoColumns.DATA + "=?";
    Cursor c = context.getContentResolver().query(uri, projection, selection, selectionArgs, null);
    Log.e("cursor count", "" + c.getCount() + c);
    int vidsCount = 0;

    if (c != null) {
        vidsCount = c.getCount();
        Log.e("VIDEO count", "" + vidsCount);
        while (c.moveToNext()) {
            Log.v("TAG", c.getString(0) + " : " + c.getString(1) + " : " + c.getString(2) + " : " + c.getString(3));

            String videoid = c.getString(0);
            String path = c.getString(1);
            String disName = c.getString(2);
            String duration = c.getString(3);
            long sizeinbytes = c.getLong(4);
            int size = (int) sizeinbytes / 1024;

            model = new VideoModelLocal(videoid, disName, duration, path, path, size);
        }
        c.close();
    }
    return model;
}

终于有候补了

 public static VideoModelLocal getVideoModelFromPathViaFFMPEG(String path) {

    VideoModelLocal model = null;


    FFmpegMediaMetadataRetriever mmr = new FFmpegMediaMetadataRetriever();
    mmr.setDataSource("file://" + path);



    String duration = mmr.extractMetadata(FFmpegMediaMetadataRetriever.METADATA_KEY_DURATION);

    String disName = mmr.extractMetadata(FFmpegMediaMetadataRetriever.METADATA_KEY_FILENAME);

    String size = mmr.extractMetadata(FFmpegMediaMetadataRetriever.METADATA_KEY_FILESIZE);

    Log.e("TAG", "" + path);
    mmr.release();

    model = new VideoModelLocal("1", disName, duration, path, path, Integer.parseInt(size));

    return model;

}

更新

还需要在gradle中添加以下代码

compile 'com.github.wseemann:FFmpegMediaMetadataRetriever:1.0.3'

在所有版本中完美运行:)