Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/189.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/video/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android MediaStore嵌入式视频_Android_Video_Mediastore - Fatal编程技术网

Android MediaStore嵌入式视频

Android MediaStore嵌入式视频,android,video,mediastore,Android,Video,Mediastore,因此,我们的应用程序可以选择拍摄照片或视频。如果用户拍照,我们可以使用MediaStore.Images.Media.insertImage功能将新图像(通过文件路径)添加到手机的多媒体资料中,并生成content://style URI。对于捕获的视频,如果我们只有它的文件路径,是否有类似的过程?我也很感兴趣,您能找到解决方案吗 编辑:解决方案是RTFM。根据“内容提供者”一章,这里是我的代码: // Save the name and description of a vid

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

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

编辑:解决方案是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));

如果您的应用程序正在生成一个新视频,并且您只想为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)开始,这种方法不再有效。

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

无论何时添加文件,都要让MediaStore内容提供商使用

sendBroadcast(new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE, Uri.fromFile(imageAdded)));
getContentResolver().delete(uri, null, null)
主要优点:可以使用MediaStore支持的任何mime类型

无论何时删除文件,都要让MediaStore内容提供商使用

sendBroadcast(new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE, Uri.fromFile(imageAdded)));
getContentResolver().delete(uri, null, null)

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

 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

我无法获得
意图。根据API 21(棒棒糖),ACTION\u MEDIA\u SCAN\u SCAN\u文件
广播对我有效,但
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);
                    }
                } );

可能的副本。我在那里发布了一个回复。回复:发布的解决方案。请注意,Java有BufferedInputStream和BufferedOutputStream,所以您不必自己做这件事。我猜它只适用于已经在媒体商店中的视频,所以它更像是查询而不是插入。。。VID_20140313_114321和路径表明,因为它是拍摄视频时标准摄像头应用程序的标准路径和命名。是的,整个媒体存储/提供程序系统在4.4中进行了大修。任何以4.4+为目标的人都应该寻找更现代的解决方案。FWIW,我认为这并不意味着对这个老问题的答案进行否决表决。谢谢你的回答,很抱歉否决表决,但因为这个答案,我花了大约0.5个小时没有去任何地方,所以我认为让其他安卓开发者也不要这样做会很好。我希望你能理解。我不认为针对较低的API在这里会有什么不同,因为Android团队不会花任何时间为未记录/不受支持的行为创建兼容性度量,而这种行为恰好在早些时候起作用。删除这一答案是否会使这些观点从否决票中恢复?如果没有,我也可以在删除之前删除下一票。哦,发送广播对我来说很有效:即使在Android P上仍然有效,我想知道为什么提到它不会在4.4+上工作,关于获取内容样式URI的问题。你怎么知道的?我想扫描一个视频文件。这对你有影响吗?@qix-Oh。。我只是试着用图像。将尝试使用视频,但现在没有机会进行测试。在调用此文件之前,是否应将文件存储在特定位置?AFAIK文件可以位于任何位置