相关疑难解决方法(0)

在 Android 10 上将视频保存并插入到图库

我正在尝试将视频保存到图库,以下代码适用于除Android Q之外的所有 Android 版本:

private void getPath() {
        String videoFileName = "video_" + System.currentTimeMillis() + ".mp4";
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
        ContentResolver resolver = getContentResolver();
        ContentValues contentValues = new ContentValues();
        contentValues.put(MediaStore.MediaColumns.DISPLAY_NAME, videoFileName);
        contentValues.put(MediaStore.MediaColumns.MIME_TYPE, "video/mp4");
        contentValues.put(MediaStore.MediaColumns.RELATIVE_PATH, "Movies/" + "Folder");
        contentValues.put(MediaStore.Video.Media.IS_PENDING, 1);
        Uri collection = MediaStore.Video.Media.getContentUri(MediaStore.VOLUME_EXTERNAL_PRIMARY);
        Uri videoUri = resolver.insert(collection, contentValues);
        if (videoUri != null) {
            try (ParcelFileDescriptor pfd = resolver.openFileDescriptor(videoUri, "w", null)) {
                OutputStream outputStream = null;
                if (pfd != null) {
                    outputStream = resolver.openOutputStream(videoUri);
                    outputStream.flush();
                    outputStream.close();
                }
            } …
Run Code Online (Sandbox Code Playgroud)

java android mediastore android-mediascanner android-10.0

3
推荐指数
2
解决办法
6927
查看次数