我试图在我正在构建的应用中分享视频(到社交媒体)。我正在使用我需要Uri解析的意图。我试图在简单的文件管理器上选择项目(列表活动),然后在长按时共享它们。因此,我需要以下代码来获取视频的Uri以便在意图中使用它。
ContentResolver contentResolver = ctx.getContentResolver();
String videoUriStr = null;
long videoId = -1;
Uri videosUri = MediaStore.Video.Media.EXTERNAL_CONTENT_URI;
String[] projection = { MediaStore.Video.VideoColumns._ID };
// TODO This will break if we have no matching item in the MediaStore.
Cursor cursor = contentResolver.query(videosUri, projection,
MediaStore.Video.VideoColumns.DATA + " =?",
new String[] { fileToShare }, null);
cursor.moveToFirst();
int columnIndex = cursor.getColumnIndex(projection[0]);
videoId = cursor.getLong(columnIndex);
cursor.close();
if (videoId != -1)
videoUriStr = videosUri.toString() + "/" + videoId;
{
return videoUriStr;
}
更新 ListActivity文件管理器文件中的前几个项目将正确共享。但最新的视频显示错误。
错误:android.database.CursorIndexOutOfBoundsException:请求索引0,大小为0
答案 0 :(得分:1)
你的光标有0行。因此,行cursor.getLong(columnIndex);
会抛出CursorIndexOutOfBoundsException
。请检查是否为null并检查是否存在任何行。
如果没有行cursor.moveToFirst()
将返回false
。
Cursor cursor = contentResolver.query(videosUri, projection,
MediaStore.Video.VideoColumns.DATA + " =?",
new String[] { fileToShare }, null);
if (cursor != null) {
if (cursor.moveToFirst()) {
int columnIndex = cursor.getColumnIndex(projection[0]);
videoId = cursor.getLong(columnIndex);
cursor.close();
if (videoId != -1)
{
videoUriStr = videosUri.toString() + "/" + videoId;
}
}
}
return videoUriStr;
MediaStore.Video.VideoColumns.DATA
字段包含视频文件的路径。因此,如果您使用uri(content://mnt/sdcard/Videos/test.mp4
)来获取文件,请将其更改为文件路径(/mnt/sdcard/Videos/test.mp4
)。
答案 1 :(得分:0)
使用while循环检查游标是否有数据:
cursor.moveToFirst();
int columnIndex = cursor.getColumnIndex(projection[0]);
videoId = cursor.getLong(columnIndex);
cursor.close();
替换为此代码:
while (cursor.moveToNext()){
int columnIndex = cursor.getColumnIndex(projection[0]);
videoId = cursor.getLong(columnIndex);
}
cursor.close();
答案 2 :(得分:0)
Error: android.database.CursorIndexOutOfBoundsException: Index 0 requested, with a size of 0
表示你的光标没有数据。使用check for cursor emptiness:
if (cursor.moveToFirst()) {
int columnIndex = cursor.getColumnIndex(projection[0]);
videoId = cursor.getLong(columnIndex);
}
cursor.close();
答案 3 :(得分:0)
此方案不依赖于手机到手机。问题是你的光标没有数据。
你可以使用cursor.moveToFirst(),如果光标为空则返回false,或者使用cursor.getCount()来知道光标中的行数。
e.g
if (cursor.moveToFirst()) {
int columnIndex = cursor.getColumnIndex(projection[0]);
videoId = cursor.getLong(columnIndex);
}
cursor.close();
或
if (cursor.getCount() > 0) {
int columnIndex = cursor.getColumnIndex(projection[0]);
videoId = cursor.getLong(columnIndex);
}
cursor.close();
答案 4 :(得分:0)
首先,您需要检查光标的大小。
if(cursor.size()>0)
{ //在这里做
cursor.moveToFirst();
int columnIndex = cursor.getColumnIndex(projection[0]);
videoId = cursor.getLong(columnIndex);
cursor.close();
if (videoId != -1)
videoUriStr = videosUri.toString() + "/" + videoId;
{
return videoUriStr;
}
}
答案 5 :(得分:0)
我能够解决我的问题,但我仍然不明白为什么我遇到旧代码的问题。感谢您的帮助。
public static Uri getVideoContentUriFromFilePath(Context context, File fileToShare) {
String filePath = fileToShare.getAbsolutePath();
Cursor cursor = context.getContentResolver().query(
MediaStore.Video.Media.EXTERNAL_CONTENT_URI,
new String[] { MediaStore.Video.Media._ID },
MediaStore.Video.Media.DATA + "=? ",
new String[] { filePath }, null);
if (cursor != null && cursor.moveToFirst()) {
int id = cursor.getInt(cursor.getColumnIndex(MediaStore.MediaColumns._ID));
return Uri.withAppendedPath(MediaStore.Video.Media.EXTERNAL_CONTENT_URI, "" + id);
} else {
if (fileToShare.exists()) {
ContentValues values = new ContentValues();
values.put(MediaStore.Video.Media.DATA, filePath);
return context.getContentResolver().insert(
MediaStore.Video.Media.EXTERNAL_CONTENT_URI, values);
} else {
return null;
}
}
}