Android MediaStore 插入视频

发布于 2024-08-19 06:38:17 字数 149 浏览 5 评论 0原文

因此我们的应用程序可以选择拍摄照片或视频。如果用户拍照,我们可以使用 MediaStore.Images.Media.insertImage 函数将新图像(通过文件路径)添加到手机的图库中并生成 content:// 样式的 URI。鉴于我们只有文件路径,捕获的视频是否有类似的过程?

So our app has the option to take either a picture or a video. If the user takes a picture, we can use the MediaStore.Images.Media.insertImage function to add the new image (via a filepath) to the phone's gallery and generate a content:// style URI. Is there a similar process for a captured video, given that we only have it's filepath?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

独自唱情﹋歌 2024-08-26 06:38:17

这是一个简单的“基于单个文件的解决方案”:

每当您添加文件时,让 MediaStore Content Provider 了解它,

sendBroadcast(new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE, Uri.fromFile(imageAdded)));

主要优点:使用 MediaStore 支持的任何 mime 类型

每当您删除< /strong> 一个文件,让 MediaStore Content Provider 使用以下方式了解它

getContentResolver().delete(uri, null, null)

Here is an easy 'single file based solution':

Whenever you add a file, let MediaStore Content Provider knows about it using

sendBroadcast(new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE, Uri.fromFile(imageAdded)));

Main advantage: work with any mime type supported by MediaStore

Whenever you delete a file, let MediaStore Content Provider knows about it using

getContentResolver().delete(uri, null, null)
路还长,别太狂 2024-08-26 06:38:17

我也感兴趣,请问能找到解决办法吗?

编辑:解决方案是RTFM。基于“内容提供商”一章,这里是我的有效代码:

        // Save the name and description of a video in a ContentValues map.  
        ContentValues values = new ContentValues(2);
        values.put(MediaStore.Video.Media.MIME_TYPE, "video/mp4");
        // values.put(MediaStore.Video.Media.DATA, f.getAbsolutePath()); 

        // Add a new record (identified by uri) without the video, but with the values just set.
        Uri uri = getContentResolver().insert(MediaStore.Video.Media.EXTERNAL_CONTENT_URI, values);

        // Now get a handle to the file for that record, and save the data into it.
        try {
            InputStream is = new FileInputStream(f);
            OutputStream os = getContentResolver().openOutputStream(uri);
            byte[] buffer = new byte[4096]; // tweaking this number may increase performance
            int len;
            while ((len = is.read(buffer)) != -1){
                os.write(buffer, 0, len);
            }
            os.flush();
            is.close();
            os.close();
        } catch (Exception e) {
            Log.e(TAG, "exception while writing video: ", e);
        } 

        sendBroadcast(new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE, uri));

I'm also interested, could you find a solution?

Edit: solution is RTFM. Based on the "Content Providers" chapter here is my code that worked:

        // Save the name and description of a video in a ContentValues map.  
        ContentValues values = new ContentValues(2);
        values.put(MediaStore.Video.Media.MIME_TYPE, "video/mp4");
        // values.put(MediaStore.Video.Media.DATA, f.getAbsolutePath()); 

        // Add a new record (identified by uri) without the video, but with the values just set.
        Uri uri = getContentResolver().insert(MediaStore.Video.Media.EXTERNAL_CONTENT_URI, values);

        // Now get a handle to the file for that record, and save the data into it.
        try {
            InputStream is = new FileInputStream(f);
            OutputStream os = getContentResolver().openOutputStream(uri);
            byte[] buffer = new byte[4096]; // tweaking this number may increase performance
            int len;
            while ((len = is.read(buffer)) != -1){
                os.write(buffer, 0, len);
            }
            os.flush();
            is.close();
            os.close();
        } catch (Exception e) {
            Log.e(TAG, "exception while writing video: ", e);
        } 

        sendBroadcast(new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE, uri));
一个人的旅程 2024-08-26 06:38:17

如果您的应用正在生成新视频,并且您只想为 MediaStore 提供一些元数据,则可以在此功能的基础上进行构建:

public Uri addVideo(File videoFile) {
    ContentValues values = new ContentValues(3);
    values.put(MediaStore.Video.Media.TITLE, "My video title");
    values.put(MediaStore.Video.Media.MIME_TYPE, "video/mp4");
    values.put(MediaStore.Video.Media.DATA, videoFile.getAbsolutePath());
    return getContentResolver().insert(MediaStore.Video.Media.EXTERNAL_CONTENT_URI, values);
}

编辑:从 Android 4.4 (KitKat) 开始,此方法不再有效。

If your app is generating a new video and you simply want to give the MediaStore some metadata for it, you can build on this function:

public Uri addVideo(File videoFile) {
    ContentValues values = new ContentValues(3);
    values.put(MediaStore.Video.Media.TITLE, "My video title");
    values.put(MediaStore.Video.Media.MIME_TYPE, "video/mp4");
    values.put(MediaStore.Video.Media.DATA, videoFile.getAbsolutePath());
    return getContentResolver().insert(MediaStore.Video.Media.EXTERNAL_CONTENT_URI, values);
}

EDIT: As of Android 4.4 (KitKat), this method no longer works.

淡淡的优雅 2024-08-26 06:38:17

我无法让 Intent.ACTION_MEDIA_SCANNER_SCAN_FILE 广播在 API 21 (Lollipop) 下为我工作,但 MediaScannerConnection 确实可以工作,例如:

        MediaScannerConnection.scanFile(
                context, new String[] { path }, null,
                new MediaScannerConnection.OnScanCompletedListener() {
                    public void onScanCompleted(String path, Uri uri) {
                        Log.d(TAG, "Finished scanning " + path + " New row: " + uri);
                    }
                } );

I was unable to get the Intent.ACTION_MEDIA_SCANNER_SCAN_FILE broadcast to work for me under API 21 (Lollipop), but the MediaScannerConnection does work, e.g.:

        MediaScannerConnection.scanFile(
                context, new String[] { path }, null,
                new MediaScannerConnection.OnScanCompletedListener() {
                    public void onScanCompleted(String path, Uri uri) {
                        Log.d(TAG, "Finished scanning " + path + " New row: " + uri);
                    }
                } );
各自安好 2024-08-26 06:38:17

试试这个代码。这似乎对我有用。

 filePath = myfile.getAbsolutePath();
 ContentValues values = new ContentValues();
 values.put(MediaStore.Video.Media.DATA, filePath);
 return context.getContentResolver().insert(
                    MediaStore.Video.Media.EXTERNAL_CONTENT_URI, values);

文件路径示例 -

/storage/emulated/0/DCIM/Camera/VID_20140313_114321.mp4

Try this code. It seems working for me.

 filePath = myfile.getAbsolutePath();
 ContentValues values = new ContentValues();
 values.put(MediaStore.Video.Media.DATA, filePath);
 return context.getContentResolver().insert(
                    MediaStore.Video.Media.EXTERNAL_CONTENT_URI, values);

Example of filePath -

/storage/emulated/0/DCIM/Camera/VID_20140313_114321.mp4
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文