将视频插入图库[Android Q] [英] Insert Video to gallery [Android Q]

查看:146
本文介绍了将视频插入图库[Android Q]的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

要记录 SurfeceView ,我正在使用第三方的,此库需要一个路径,在本例中,保存的输出(录制的视频)是 savedVideoPath :

To record a SurfeceView I'm using a 3rd-party library , this library requires a path where the output (the recorded video) saved in my case is savedVideoPath :

 mRenderPipeline = EZFilter.input(this.effectBmp)
                    .addFilter(new Effects().getEffect(VideoMaker.this, i))
                    .enableRecord(savedVideoPath, true, false)
                    .into(mRenderView);

停止录制后,应使用 savedVideoPath 作为路径保存视频,当我测试代码时,也就是说,当我打开Gallery应用程序时,我会在此处看到保存的视频,但是当我在Android Q上进行测试时,我什么都看不到.

After the recording stopped, the video should be saved with savedVideoPath as a path, when I test the code, that is to say , when I open the gallery app, I see the saved video there, but when I tested on Android Q, I can't see anything.

由于不赞成使用 getExternalStoragePublicDirectory getExternalStorageDirectory ,我尝试使用 getExternalFilesDir 如下:

Since getExternalStoragePublicDirectory and getExternalStorageDirectory are deprecated ,I tried to use getExternalFilesDir as following :

private void getPath() {
        String videoFileName = "video_" + System.currentTimeMillis() + ".mp4";
        fileName = videoFileName;
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
            File imageFile = null;
            File storageDir = new File(getExternalFilesDir(Environment.DIRECTORY_MOVIES), "Folder");
            source = storageDir;
            boolean success = true;
            if (!storageDir.exists()) {
                success = storageDir.mkdirs();
            }
            if (success) {
                imageFile = new File(storageDir, videoFileName);
                savedVideoPath = imageFile.getAbsolutePath();
            }
        } else {
            File storageDir = new File(
                    Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_MOVIES)
                            + "/Folder");
            boolean success = true;
            if (!storageDir.exists()) {
                success = storageDir.mkdirs();
            }
            if (success) {
                File videoFile = new File(storageDir, videoFileName);
                savedVideoPath = videoFile.getAbsolutePath();
            }
        }
    }

录制停止后,我进入 Files Explorer应用> Android>数据> com.packageName>文件>电影>文件夹,我可以在那里看到所有保存的视频,但是看不到他们在画廊.

After the recording stopped, I go to Files Explorer app > Android > data > com.packageName > files > Movies > Folder ,I can see all saved videos there,but I can't see them on the gallery.

我尝试使用 Intent.ACTION_MEDIA_SCANNER_SCAN_FILE 刷新图库,但不幸的是,它不起作用.

I tried to use Intent.ACTION_MEDIA_SCANNER_SCAN_FILE to refresh the gallery, but unfortunately doesn't work.

我还尝试了 MediaScannerConnection :

 MediaScannerConnection.scanFile(context, new String[]{savedVideoPath}, new String[]{"video/mp4"}, new MediaScannerConnection.MediaScannerConnectionClient() {
            public void onMediaScannerConnected() {
            }

            public void onScanCompleted(String s, Uri uri) {

            }
        });

  • 有人可以帮助我解决此问题吗?我坚持了将近2天
  • 推荐答案

    您必须更改库才能使其与Android Q兼容.如果无法执行此操作,则可以将视频复制到媒体库,然后删除在getExternalFilesDir()中创建的旧视频.在此之后,您将拥有视频的uri,并且可以使用uri做您想做的事

    You have to change the library to make it work with Android Q . If you cannot do this you could copy the video to the media gallery and then delete the old video created in getExternalFilesDir(). After this you have the uri of the video and can do what you want with the uri

    如果您已使用getExternalFilesDir()保存了视频,则可以在此处使用我的示例:您获得的媒体uri是"uriSavedVideo"..这只是一个例子.大型视频也应该在后台复制.

    If you have saved the video with getExternalFilesDir() you could use my example here : The media uri you get is "uriSavedVideo" . This is only an example. A large video should also be copied in the background.

    Uri uriSavedVideo;
        File createdvideo = null;
        ContentResolver resolver = getContentResolver();
        
        
    
        String videoFileName = "video_" + System.currentTimeMillis() + ".mp4";
    
        ContentValues valuesvideos;
        valuesvideos = new ContentValues();
    
    
        if (Build.VERSION.SDK_INT >= 29) {
    
            valuesvideos.put(MediaStore.Video.Media.RELATIVE_PATH, "Movies/" + "Folder");
    
            valuesvideos.put(MediaStore.Video.Media.TITLE, videoFileName);
            valuesvideos.put(MediaStore.Video.Media.DISPLAY_NAME, videoFileName);
            valuesvideos.put(MediaStore.Video.Media.MIME_TYPE, "video/mp4");
            valuesvideos.put(MediaStore.Video.Media.DATE_ADDED, System.currentTimeMillis() / 1000);
    
            Uri collection = MediaStore.Video.Media.getContentUri(MediaStore.VOLUME_EXTERNAL_PRIMARY);
            uriSavedVideo = resolver.insert(collection, valuesvideos);
    
    
        } else {
    
            String directory  = Environment.getExternalStorageDirectory().getAbsolutePath() + File.separator+ Environment.DIRECTORY_MOVIES + "/"+"YourFolder";
    
            createdvideo = new File(directory, videoFileName);
    
            valuesvideos.put(MediaStore.Video.Media.TITLE, videoFileName);
            valuesvideos.put(MediaStore.Video.Media.DISPLAY_NAME, videoFileName);
            valuesvideos.put(MediaStore.Video.Media.MIME_TYPE, "video/mp4");
            valuesvideos.put(MediaStore.Video.Media.DATE_ADDED, System.currentTimeMillis() / 1000);
    
            valuesvideos.put(MediaStore.Video.Media.DATA, createdvideo.getAbsolutePath());
            uriSavedVideo = getContentResolver().insert(MediaStore.Video.Media.EXTERNAL_CONTENT_URI, valuesvideos);
    
    
        }
    
    
        if (Build.VERSION.SDK_INT >= 29) {
            valuesvideos.put(MediaStore.Video.Media.DATE_TAKEN, System.currentTimeMillis());
            valuesvideos.put(MediaStore.Video.Media.IS_PENDING, 1);
    
    
        }
    
    
      
        
        
        ParcelFileDescriptor pfd;
    
        try {
            pfd = getContentResolver().openFileDescriptor(uriSavedVideo, "w");
    
            FileOutputStream out = new FileOutputStream(pfd.getFileDescriptor());
    
            // get the already saved video as fileinputstream
    
            // The Directory where your file is saved
            File storageDir = new File(getExternalFilesDir(Environment.DIRECTORY_MOVIES), "Folder");
    
    
            //Directory and the name of your video file to copy
            File videoFile = new File(storageDir, "Myvideo"); 
    
            FileInputStream in = new FileInputStream(videoFile);
    
    
            byte[] buf = new byte[8192];
            int len;
            while ((len = in.read(buf)) > 0) {
    
                out.write(buf, 0, len);
            }
    
    
            out.close();
            in.close();
            pfd.close();
    
    
    
    
        } catch (Exception e) {
    
            e.printStackTrace();
        }
    
    
    
    
    
        if (Build.VERSION.SDK_INT >= 29) {
            valuesvideos.clear();
            valuesvideos.put(MediaStore.Video.Media.IS_PENDING, 0);
            getContentResolver().update(uriSavedVideo, valuesvideos, null, null);
    
        }
    

    这篇关于将视频插入图库[Android Q]的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆