I'm trying to retrieve the metadata from a video file (title, language, artist) using the method MediaStore.Video.query(). However, the method is always returning null. The code is bellow:
String[] columns = {
MediaStore.Video.VideoColumns._ID,
MediaStore.Video.VideoColumns.TITLE,
MediaStore.Video.VideoColumns.ARTIST
};
Cursor cursor = MediaStore.Video.query(getApplicationContext().getContentResolver(), videoUri,columns);
if (cursor != null) {
cursor.moveToNext();
}
String title = cursor.getString(cursor.getColumnIndex(MediaStore.Video.VideoColumns.TITLE));
Any suggestion about how to return video metadata using android?
==Update
As I searched in many places, I tried one solution using CursorLoader. However, the method loadInBackground() from CursorLoader is also returning null. The code is showed bellow:
String[] columns = {
MediaStore.Video.VideoColumns.TITLE
};
Uri videoUri = Uri.parse("content://mnt/sdcard/Movies/landscapes.mp4");
CursorLoader loader = new CursorLoader(getBaseContext(), videoUri, columns, null, null, null);
Cursor cursor = loader.loadInBackground();
cursor.moveToFirst();
String title = cursor.getString(cursor.getColumnIndex(MediaStore.Video.VideoColumns.TITLE));
Uri.parse("content://mnt/sdcard/Movies/landscapes.mp4")
is not an Uri forMediaStore
. It would try to find aContentProvider
for authoritymnt
which does not exist.MediaStore
can handle onlycontent://media/...
Uris which you should get exclusively viaMediaStore
, not by usingUri.parse()
.In your case use the following for example
The
MediaStore.Video.VideoColumns.DATA
field holds the path to the videos and you search for a certain video this way. At least for now, future versions of Android may change that.Your second example is using
CursorLoader
the wrong way. If you callloader.loadInBackground()
yourself, you load the data in foreground. See e.g. http://mobile.tutsplus.com/tutorials/android/android-sdk_loading-data_cursorloader/The next thing you do is
This will lead to a
CursorIndexOutOfBoundsException
if yourcursor
has 0 rows andcursor.moveToFirst()
failed because there is no first row. The cursor stays before the first row (at -1) and that index does not exist. That would mean in your case that the file was not found in the database.To prevent that use the return value of
moveToFirst
- it will only betrue
if there is a first row.A more complete example including checks for
null
and closing thecursor
in all casesI guess the file you try to find is either not indexed in the database (rebooting forces the indexer to run again) or the path is wrong.
Or the path you use is actually a symlink in which case MediaStore might use a different path.
Use this to get rid of symlinks
cursor.getColumnCount()
is the column count, not the row count. It should always be the same as the number of columns you requested incolumns
. You need to checkcursor.getCount()
if you want to check the row count.Try dumping all the videos known to MediaStore into logcat in case it does not show as expected.
I updated your code, it works, just check it