我正在尝试将视频文件添加到媒体内容提供商。当我这样做时,插入视频但其文件路径出错。视频显示在图库中,但如果您尝试从图库中播放,则会显示“无法播放内容”错误。该文件是已经在库中的文件的副本,原始文件可以正常播放。
那是:
如上所述,视频显示在图库中(两次:DCIM文件夹中的原始视频加上复制的视频),但无法播放副本。
这是代码。我不是自己写的,它实际上来自ODK Collect应用程序,原始的ODK Collect应用程序(在我的修改之前)也不起作用:
//binaryuri comes from recording a video with the Camera component as an intent
String binaryPath = getPathFromUri((Uri) binaryuri);
String extension = binaryPath.substring(binaryPath.lastIndexOf("."));
String destVideoPath = mInstanceFolder + "/" + System.currentTimeMillis() + extension;
File source = new File(binaryPath);
File newVideo = new File(destVideoPath);
//this simply copies the file into a new location, and it works fine:
FileUtils.copyFile(source, newVideo);
if (newVideo.exists()) {
// Add the copy to the content provier
ContentValues values = new ContentValues(6);
values.put(Video.Media.TITLE, newVideo.getName());
values.put(Video.Media.DISPLAY_NAME, newVideo.getName());
values.put(Video.Media.DATE_ADDED, System.currentTimeMillis());
values.put(Video.Media.DATA, newVideo.getAbsolutePath());
Log.d(t,"Inserting VIDEO: data: "+values.getAsString(Video.Media.DATA));
Uri VideoURI =
getContext().getContentResolver().insert(Video.Media.EXTERNAL_CONTENT_URI, values);
Log.i(t, "Inserting VIDEO returned uri = " + VideoURI.toString());
// ** THIS PART I ADDED FOR DEBUGGING PURPOSES ONLY **
// check: (for debug only)
Cursor c=getContext().getContentResolver().query(VideoURI, null, null, null, null);
if (c.moveToFirst()) {
Log.i(t,"The data path of the inserted video is "+c.getString(c.getColumnIndex(Video.Media.DATA)));
}
// check: (for debug only)
c=getContext().getContentResolver().query(Video.Media.EXTERNAL_CONTENT_URI, null,Video.Media.DATA +" = '"+newVideo.getAbsolutePath()+"'", null, null);
if (c.moveToFirst()) {
Log.d(t,"Double checked: video exists in media content provider");
}
else {
Log.d(t,"Can't find the newly inserted video by file path");
}
} else {
Log.e(t, "Inserting Video file FAILED");
}
这会在日志中生成以下输出:
02-18 18:14:54.888 D / MediaWidget(12752):插入视频:数据: / mnt / sdcard / megafone / instances / Matteo2 New York_2012-02-18_18-14-37 / 1329585294726.3gp 02-18 18:14:54.908 I / MediaWidget(12752):插入VIDEO返回uri = content:// media / external / video / media / 5 02-18 18:14:54.918 I / MediaWidget(12752):插入视频的数据路径为 / mnt / sdcard / megafone / instances / Matteo2 New York_2012-02-18_18-14-37 / 1329585294726.3gp 02-18 18:14:54.928 D / MediaWidget(12752):无法按文件路径找到新插入的视频
这让我发疯了。在插入文件后,我查询内容提供程序并检查其文件路径,它实际上是我给它的相同值。然而,紧接着,我在数据库中查找具有DATA字段的值的项目,并且找不到视频(并且查询是正常的:我对图像执行相同操作)。
有关插入视频为何被破坏以及如何解决此问题的想法?