การรับข้อมูลวิดีโอจากที่เก็บข้อมูลไม่ทำงาน

ฉันกำลังพยายามรับความกว้างและขนาดวิดีโอโดยใช้โค้ดง่ายๆ นี้:

String[] filePathColumn = {MediaStore.Video.VideoColumns.DATA,
                        MediaStore.Video.VideoColumns.WIDTH,
                        MediaStore.Video.VideoColumns.HEIGHT};
cursor = mContext.getContentResolver().query(mVideoUri, filePathColumn, null, null, null);
if (cursor != null && cursor.moveToFirst()) {
      mVideoDecodableString = cursor.getString(cursor.getColumnIndex(filePathColumn[0]));
      mVideoWidth = cursor.getInt(cursor.getColumnIndex(filePathColumn[1]));
      mVideoHeight = cursor.getInt(cursor.getColumnIndex(filePathColumn[2]));
}

น่าเสียดายที่ฉันมักจะได้รับความกว้างและความสูง 0 เสมอสำหรับไฟล์วิดีโอทุกไฟล์ในแกลเลอรีของฉัน แต่การรับข้อมูลวิดีโอใช้งานได้

ฉันกำลังทำอะไรผิดหรือทำไม่ได้ใน Android


person Nominalista    schedule 10.12.2016    source แหล่งที่มา


คำตอบ (1)


คุณสามารถใช้ MediaMetadataRetriever เพื่อดึงข้อมูลความสูงและความกว้างของไฟล์วิดีโอ

คุณต้องใช้วิธี extractMetadata() โดยใช้ค่าคงที่ METADATA_KEY_VIDEO_HEIGHT และ METADATA_KEY_VIDEO_WIDTH ดังตัวอย่างด้านล่าง

MediaMetadataRetriever metaRetriever = new MediaMetadataRetriever();
metaRetriever.setDataSource(/* file path goes here. eg."/path/to/video.mp4" */);
String height = metaRetriever.extractMetadata(MediaMetadataRetriever.METADATA_KEY_VIDEO_HEIGHT);
String width = metaRetriever.extractMetadata(MediaMetadataRetriever.METADATA_KEY_VIDEO_WIDTH);

หมายเหตุ: MediaMetadataRetriever ต้องการ API ระดับ 10 หรือสูงกว่า

แก้ไข:

ฉันไม่แน่ใจ แต่ฉันคิดว่าคุณต้องใช้ RESOLUTION คอลัมน์เพื่อรับความละเอียด (กว้าง × สูง) ของไฟล์วิดีโอในรูปแบบ ContentResolver เหมือนตัวอย่างด้านล่าง

String[] projection = new String[] {MediaStore.Video.VideoColumns.RESOLUTION};
Cursor cursor = context.getContentResolver().query(uri, projection, null, null, null);
if (cursor.moveToFirst()) {
    String resolution = cursor.getString(0);
    if(!StringUtils.isEmpty(resolution)) {
        int index = resolution.indexOf('x');
        width = Integer.parseInt(resolution.substring(0, index));
        height = Integer.parseInt(resolution.substring(index + 1));
}
person Priyank Patel    schedule 10.12.2016
comment
มันทำงานได้อย่างสมบูรณ์แบบ คำถามของฉันคือทำไมโค้ดในคำถามถึงใช้งานไม่ได้ หากคุณสามารถอธิบายได้ฉันจะยอมรับคำตอบของคุณ - person Nominalista; 10.12.2016
comment
@ThirdMartian ตรวจสอบคำตอบที่แก้ไขของฉันเพื่อรับแบบฟอร์มความละเอียดวิดีโอ ContentResolver - person Priyank Patel; 10.12.2016