Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p><code>Uri.parse("content://mnt/sdcard/Movies/landscapes.mp4")</code> is not an Uri for <code>MediaStore</code>. It would try to find a <code>ContentProvider</code> for authority <code>mnt</code> which does not exist.</p> <p><code>MediaStore</code> can handle only <code>content://media/...</code> Uris which you should get exclusively via <code>MediaStore</code>, not by using <code>Uri.parse()</code>.</p> <p>In your case use the following for example</p> <pre><code>Uri uri = MediaStore.Video.Media.EXTERNAL_CONTENT_URI; String[] columns = { MediaStore.Video.VideoColumns._ID, MediaStore.Video.VideoColumns.TITLE, MediaStore.Video.VideoColumns.ARTIST }; String selection = MediaStore.Video.VideoColumns.DATA + "=?"; String selectionArgs[] = { "/mnt/sdcard/Movies/landscapes.mp4" }; Cursor cursor = context.getContentResolver().query(uri, columns, selection, selectionArgs, null); </code></pre> <p>The <code>MediaStore.Video.VideoColumns.DATA</code> 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.</p> <hr> <p>Your second example is using <code>CursorLoader</code> the wrong way. If you call <code>loader.loadInBackground()</code> yourself, you load the data in foreground. See e.g. <a href="http://mobile.tutsplus.com/tutorials/android/android-sdk_loading-data_cursorloader/">http://mobile.tutsplus.com/tutorials/android/android-sdk_loading-data_cursorloader/</a> </p> <p>The next thing you do is </p> <pre><code> Cursor cursor = getCursor(); cursor.moveToFirst(); String title = cursor.getString(/* some index */); </code></pre> <p>This will lead to a <code>CursorIndexOutOfBoundsException</code> if your <code>cursor</code> has 0 rows and <code>cursor.moveToFirst()</code> 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.</p> <p>To prevent that use the return value of <code>moveToFirst</code> - it will only be <code>true</code> if there is a first row.</p> <pre><code> Cursor cursor = getCursor(); // from somewhere if (cursor.moveToFirst()) { String title = cursor.getString(/* some index */); } </code></pre> <p>A more complete example including checks for <code>null</code> and closing the <code>cursor</code> in all cases</p> <pre><code> Cursor cursor = getCursor(); // from somewhere String title = "not found"; if (cursor != null) { if (cursor.moveToFirst()) { title = cursor.getString(/* some index */); } cursor.close(); } </code></pre> <p>I 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.</p> <p>Or the path you use is actually a symlink in which case MediaStore might use a different path.</p> <p>Use this to get rid of symlinks</p> <pre><code> String path = "/mnt/sdcard/Movies/landscapes.mp4"; try { path = new File(path).getCanonicalPath(); } catch (IOException e) { e.printStackTrace(); } </code></pre> <hr> <blockquote> <p>Yes, I tested now and it is throwing IndexOutOfBoundsException. When I'm using cursor.getColumnCount() it returns 1</p> </blockquote> <p><code>cursor.getColumnCount()</code> is the column count, not the row count. It should always be the same as the number of columns you requested in <code>columns</code>. You need to check <code>cursor.getCount()</code> if you want to check the row count.</p> <hr> <p>Try dumping all the videos known to MediaStore into logcat in case it does not show as expected.</p> <pre><code>public static void dumpVideos(Context context) { Uri uri = MediaStore.Video.Media.EXTERNAL_CONTENT_URI; String[] projection = { MediaStore.Video.VideoColumns.DATA }; Cursor c = context.getContentResolver().query(uri, projection, null, null, null); int vidsCount = 0; if (c != null) { vidsCount = c.getCount(); while (c.moveToNext()) { Log.d("VIDEO", c.getString(0)); } c.close(); } Log.d("VIDEO", "Total count of videos: " + vidsCount); } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload