5

在我的一台设备上,以下代码无法让我的视频出现在图库中:

File file = new File(path);
Uri uri = Uri.fromFile(file);
Intent scanFileIntent = new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE, uri);
activity.sendBroadcast(scanFileIntent);

所以我scanFile明确使用:

android.media.MediaScannerConnection.scanFile(activity, new String[]{file.getAbsolutePath()},
                new String[]{"video/" + mMimeType}, null);

当我的视频是 xxx.mpeg4 时,值为mMimeTypemp4,结果是视频可以出现在 MediaStore 中,但我之后无法获取时长(返回值始终为 0)。在这方面需要帮助。

public static long[] getVideoDetail(Context context, Uri uri) {
        long[] result = new long[] {DEFAULT_VIDEO_FRAME_WIDTH, DEFAULT_VIDEO_FRAME_HEIGHT, -1};
        if(uri == null || (!ContentResolver.SCHEME_CONTENT.equals(uri.getScheme()))) {
            return result;
        }
            String[] projection = new String[] {MediaStore.Video.Media.RESOLUTION, MediaStore.Video.VideoColumns.DURATION};
            Cursor cursor = null;
            boolean success = false;
            try {
                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');
                        result[0] = Integer.parseInt(resolution.substring(0, index));
                        result[1] = Integer.parseInt(resolution.substring(index + 1));
                        if(result[0] != 0 && result[1] != 0) {
                            success = true;
                        }
                        if(result[0] > result[1]) {
                            swap(result, 0, 1);
                        }
                    }
                    result[2] = cursor.getLong(1);
                    if(result[2] >= 0 && success) {
                        success = true;
                    } else {
                        success = false;
                    }
                }
                if (null != cursor) {
                    cursor.close();
                }
            }
            catch (Exception e) {
                // do nothing
            } finally {
                try {
                    if (null != cursor) {
                        cursor.close();
                    }
                } catch (Exception e2) {
                    // do nothing
                }
            }
            if (!success) {
                try {
                    ContentResolver contentResolver = context.getContentResolver();
                    String selection = MediaStore.Images.Media._ID + "= ?";
                    String id = uri.getLastPathSegment();
                    String[] selectionArgs = new String[]{ id };
                    cursor = contentResolver.query(MediaStore.Video.Media.EXTERNAL_CONTENT_URI, projection, selection, selectionArgs, null);
                    if (cursor.moveToFirst()) {
                        String resolution = cursor.getString(0);
                        if(!StringUtils.isEmpty(resolution)) {
                            int index = resolution.indexOf('x');
                            result[0] = Integer.parseInt(resolution.substring(0, index));
                            result[1] = Integer.parseInt(resolution.substring(index + 1));
                            if(result[0] > result[1]) {
                                swap(result, 0, 1);
                            }
                        }
                        result[2] = cursor.getLong(1);
                    }
                    if (null != cursor) {
                        cursor.close();
                    }

                } catch (Exception e) {
                    // do nothing
                } finally {
                    try {
                        if (cursor != null) {
                            cursor.close();
                        }
                    } catch (Exception e) {
                        // do nothing
                    }
                }
            }
        if(result[0] <= 0) {
            result[0] = DEFAULT_VIDEO_FRAME_WIDTH;
        }
        if(result[1] <= 0) {
            result[1] = DEFAULT_VIDEO_FRAME_HEIGHT;
        }
        return result;
    }
4

2 回答 2

2

正如我在您的代码中看到的,您在投影中请求持续时间

 String[] projection = new String[] {MediaStore.Video.Media.RESOLUTION, MediaStore.Video.VideoColumns.DURATION};

现在您只需要从cursor如下所示的内容中检索它:

 long timeInMs = cursor.getLong(cursor.getColumnIndex(MediaStore.Video.VideoColumns.DURATION));
于 2016-12-21T17:30:04.693 回答
0

从 MediaPlayer 获得帮助:

MediaPlayer mp = new MediaPlayer();
    try {
        mp.setDataSource(context, Uri.parse(uri));
    } catch (IOException e) {
        Log.d("-MS-","Cannot parse url");

        e.printStackTrace();
    }
int duration=  mp.getDuration();
String[] projection = new String[] {MediaStore.Video.Media.RESOLUTION,duration};

我总是通过 MediaPlayer 获得持续时间。

于 2014-12-07T05:45:29.863 回答