Beena
Beena

Reputation: 2354

Get video info from file path

I have video path. I am able to load video in video using this path.

Now i want some video info from db but my Cursor is always null. Below is my code.

   String videoPath=Uri.fromFile(new File("/storage/emulated/0/Android/data/files/1483767006415.mp4")
   final String[] projection = new String[]{MediaStore.Video.Media._ID, MediaStore.Video.Media.DISPLAY_NAME, MediaStore.Video.Media.DATA,
            MediaStore.Video.Media.DURATION};
    CursorLoader loader = new CursorLoader(getActivity(), contentUri, projection, null, null, null);
    Cursor cursor = loader.loadInBackground();
    if (cursor != null && cursor.moveToFirst()) {
        long id = cursor.getLong(cursor.getColumnIndex(projection[0]));
        String name = cursor.getString(cursor.getColumnIndex(projection[1]));
        String path = cursor.getString(cursor.getColumnIndex(projection[2]));
        long duration = cursor.getLong(cursor.getColumnIndex(projection[3]));
        cursor.close();
        return new ImageObject(id, name, path, false, MEDIA_TYPE_VIDEO, duration);
    }

Seems that uri is not proper.

Thanks.

Upvotes: 0

Views: 2244

Answers (1)

Beena
Beena

Reputation: 2354

Finally got answer. Issue was in my URI. Video id was not appended in URI. So i have managed to get video id first and then createdURI. After using this URI, I was able to get all info. Below is code for same.

  Uri mainUri;
    Cursor cursor1 = getContentResolver().query(
            MediaStore.Video.Media.EXTERNAL_CONTENT_URI,
            new String[]{MediaStore.Video.Media._ID},
            MediaStore.Video.Media.DATA + "=? ",
            new String[]{pathMain}, null);
    if (cursor1 != null && cursor1.moveToFirst()) {
        int id = cursor1.getInt(cursor1.getColumnIndex(MediaStore.MediaColumns._ID));
        cursor1.close();
        mainUri = Uri.withAppendedPath(MediaStore.Video.Media.EXTERNAL_CONTENT_URI, "" + id);
    } else {
        ContentValues values = new ContentValues();
        values.put(MediaStore.Video.Media.DATA, pathMain);
        mainUri = getContentResolver().insert(
                MediaStore.Video.Media.EXTERNAL_CONTENT_URI, values);

    }
    final String[] projection = new String[]{MediaStore.Video.Media._ID, MediaStore.Video.Media.DISPLAY_NAME, MediaStore.Video.Media.DATA,
            MediaStore.Video.Media.DURATION};
    String selection = MediaStore.Video.Media.DATA + "=?";
    String selectionArgs[] = {pathMain};
    CursorLoader loader = new CursorLoader(getActivity(), mainUri, projection, selection, selectionArgs, null);
    Cursor cursor = loader.loadInBackground();
    if (cursor != null && cursor.moveToFirst()) {

        long id = cursor.getLong(cursor.getColumnIndex(projection[0]));
        String name = cursor.getString(cursor.getColumnIndex(projection[1]));
        String path = cursor.getString(cursor.getColumnIndex(projection[2]));
        long duration = cursor.getLong(cursor.getColumnIndex(projection[3]));
        cursor.close();
        return new ImageObject(id, name, path, false, MEDIA_TYPE_VIDEO, duration);

    }

Upvotes: 1

Related Questions