Android MediaStore insertVideo

Rob*_*ert 7 video android mediastore

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

Pas*_*cal 10

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

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

sendBroadcast(new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE, Uri.fromFile(imageAdded)));
Run Code Online (Sandbox Code Playgroud)

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

每当您删除文件时,请让MediaStore Content Provider使用它来了解它

getContentResolver().delete(uri, null, null)
Run Code Online (Sandbox Code Playgroud)


Gab*_*bor 6

我也很感兴趣,你能找到解决方案吗?

编辑:解决方案是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));
Run Code Online (Sandbox Code Playgroud)


acj*_*acj 5

如果您的应用正在生成新视频,而您只想为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);
}
Run Code Online (Sandbox Code Playgroud)

编辑:从Android 4.4(KitKat)开始,此方法不再起作用。


qix*_*qix 5

我无法让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);
                    }
                } );
Run Code Online (Sandbox Code Playgroud)