如何在Android棉花糖中获取创建的视频文件内容



我正在通过FFMPEG在视频编辑应用程序上工作。我可以在除棉花糖外的所有Android版本中获取视频内容,例如ID,DATA,DIDATION_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;
}

update

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

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

在所有版本中完美工作:)

最新更新