Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get data from MediaStore.File and MediaStore.File.FileColumn

I am trying to get all the file name having audio files in it I have used Mediastore to get the mediastore audio,album,playlist and audio DATA also but how I can get the file or folder titles which contains the audio file .Here is the code that I have tried but it is not correct as I am not able to set the External_Content_uri. This is the code I have tried.

  private void External() {
    try {
        String[] proj = {MediaStore.Files.FileColumns.TITLE,
                MediaStore.Files.FileColumns._ID,
                MediaStore.Files.FileColumns.PARENT,
                MediaStore.Files.FileColumns.DATA
        };// Can include more data for more details and check it.

        String selection =MediaStore.Files.FileColumns.MEDIA_TYPE+"=?";

        String[] selectionArgs = {"MediaStore.Files.FileColumns.MEDIA_TYPE_AUDIO"};

        String sortOrder = MediaStore.Audio.Media.TITLE + " ASC";

        Cursor audioCursor = getContentResolver().query(MediaStore.Files.getContentUri("\"external\""), proj, selection, selectionArgs, sortOrder);

        if (audioCursor != null) {
            if (audioCursor.moveToFirst()) {
                do {
                    int filetitle = audioCursor.getColumnIndexOrThrow(MediaStore.Files.FileColumns.TITLE);
                    int file_id = audioCursor.getColumnIndexOrThrow(MediaStore.Files.FileColumns._ID);
                    int fileparent = audioCursor.getColumnIndexOrThrow(MediaStore.Files.FileColumns.PARENT);
                    int filedata = audioCursor.getColumnIndexOrThrow(MediaStore.Files.FileColumns.DATA);
                    Mediafileinfo info = new Mediafileinfo();
                    info.setData(new File(new File(audioCursor.getString(filedata)).getParent()).getName());
                    info.setTitle(audioCursor.getString(filetitle));
                    info.set_id(audioCursor.getString(file_id));
                    info.setParent(audioCursor.getString(fileparent));
                   // info.setData(audioCursor.getString(filedata));
                    audioList.add(info);
                } while (audioCursor.moveToNext());
            }
        }
        assert audioCursor != null;
        audioCursor.close();
    } catch (Exception e) {
        e.printStackTrace();
    }
}

I tried this and this example but I am not able to get the solution.

like image 326
Neelay Srivastava Avatar asked Sep 06 '16 17:09

Neelay Srivastava


2 Answers

Writing a fresh answer, since all you want is the folder names of the audio files.

So the best thing to use here is MediaStore.Audio.Media instead of using MediaStore.File. Get the folder names using the below on the audio file path.

new File(new File(audioCursor.getString(filedata)).getParent()).getName()

private void External() {
try {
    Uri externalUri= MediaStore.Audio.Media.EXTERNAL_CONTENT_URI;

    projection=new String[]{MediaStore.Audio.Media._ID,
            MediaStore.Audio.Media.TITLE,
            MediaStore.Audio.Media.DATA,;

    String selection =null;

    String[] selectionArgs = null;

    String sortOrder = MediaStore.Audio.Media.TITLE + " ASC";

    Cursor audioCursor = getContentResolver().query(externalUri, proj, selection, selectionArgs, sortOrder);

    if (audioCursor != null) {
        if (audioCursor.moveToFirst()) {
            do {
                int filetitle = audioCursor.getColumnIndexOrThrow(MediaStore.Audio.Media.TITLE);
                int file_id = audioCursor.getColumnIndexOrThrow(MediaStore.Audio.Media._ID);
                int filePath = audioCursor.getColumnIndexOrThrow(MediaStore.Audio.Media.DATA);
                Mediafileinfo info = new Mediafileinfo();
                info.setData(new File(new File(audioCursor.getString(filedata)).getParent()).getName());
                info.setTitle(audioCursor.getString(filetitle));
                info.set_id(audioCursor.getString(file_id));
                audioList.add(info);
            } while (audioCursor.moveToNext());
        }
    }
    assert audioCursor != null;
    audioCursor.close();
} catch (Exception e) {
    e.printStackTrace();
}
}
like image 122
Darshan Miskin Avatar answered Oct 08 '22 20:10

Darshan Miskin


Hi query media file like audio,images and video using Android Media Store and Android Content Resolver, check this tutorial
http://www.androiddevelopersolutions.com/2015/12/android-media-store-tutorial-list-all.html

For Listing All images:

private void parseAllImages() {
    try {
        String[] projection = {MediaStore.Images.Media.DATA};
        cursor = getContentResolver().query(MediaStore.Images.Media.EXTERNAL_CONTENT_URI,
                projection, // Which columns to return
                null,       // Return all rows
                null,
                null);


        int size = cursor.getCount();

        /*******  If size is 0, there are no images on the SD Card. *****/

        if (size == 0) {

        } else {

            int thumbID = 0;
            while (cursor.moveToNext()) {

                int file_ColumnIndex = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);

                /**************** Captured image details ************/

                /*****  Used to show image on view in LoadImagesFromSDCard class ******/
                String path = cursor.getString(file_ColumnIndex);

                String fileName = path.substring(path.lastIndexOf("/") + 1, path.length());

                MediaFileInfo mediaFileInfo = new MediaFileInfo();
                mediaFileInfo.setFilePath(path);
                mediaFileInfo.setFileName(fileName);
                mediaFileInfo.setFileType(type);
                mediaList.add(mediaFileInfo);
            }
        }


    } catch (Exception e) {
        e.printStackTrace();
    }

}

For Listing all Video(.mp4)

private void parseAllVideo() {
    try {
        String name = null;
        String[] thumbColumns = {MediaStore.Video.Thumbnails.DATA,
                MediaStore.Video.Thumbnails.VIDEO_ID};

        int video_column_index;
        String[] proj = {MediaStore.Video.Media._ID,
                MediaStore.Video.Media.DATA,
                MediaStore.Video.Media.DISPLAY_NAME,
                MediaStore.Video.Media.SIZE};
        Cursor videocursor = getContentResolver().query(MediaStore.Video.Media.EXTERNAL_CONTENT_URI,
                proj, null, null, null);
        int count = videocursor.getCount();
        Log.d("No of video", "" + count);
        for (int i = 0; i < count; i++) {
            MediaFileInfo mediaFileInfo = new MediaFileInfo();
            video_column_index = videocursor
                    .getColumnIndexOrThrow(MediaStore.Video.Media.DISPLAY_NAME);
            videocursor.moveToPosition(i);
            name = videocursor.getString(video_column_index);

            mediaFileInfo.setFileName(name);

            int column_index = videocursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
            videocursor.moveToPosition(i);
            String filepath = videocursor.getString(column_index);

            mediaFileInfo.setFilePath(filepath);
            mediaFileInfo.setFileType(type);
            mediaList.add(mediaFileInfo);
            // id += " Size(KB):" +
            // videocursor.getString(video_column_index);


        }
        videocursor.close();

    } catch (Exception e) {
        e.printStackTrace();
    }

}

For Listing All Audio

private void parseAllAudio() {
    try {
        String TAG = "Audio";
        Cursor cur = getContentResolver().query(
                MediaStore.Audio.Media.EXTERNAL_CONTENT_URI, null, null, null,
                null);

        if (cur == null) {
            // Query failed...
            Log.e(TAG, "Failed to retrieve music: cursor is null :-(");

        }
        else if (!cur.moveToFirst()) {
            // Nothing to query. There is no music on the device. How boring.
            Log.e(TAG, "Failed to move cursor to first row (no query results).");

        }else {
            Log.i(TAG, "Listing...");
            // retrieve the indices of the columns where the ID, title, etc. of the song are

            // add each song to mItems
            do {
                int artistColumn = cur.getColumnIndex(MediaStore.Audio.Media.ARTIST);
                int titleColumn = cur.getColumnIndex(MediaStore.Audio.Media.TITLE);
                int albumColumn = cur.getColumnIndex(MediaStore.Audio.Media.ALBUM);
                int durationColumn = cur.getColumnIndex(MediaStore.Audio.Media.DURATION);
                int idColumn = cur.getColumnIndex(MediaStore.Audio.Media._ID);
                int filePathIndex = cur.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
                Log.i(TAG, "Title column index: " + String.valueOf(titleColumn));
                Log.i(TAG, "ID column index: " + String.valueOf(titleColumn));

                Log.i("Final ", "ID: " + cur.getString(idColumn) + " Title: " + cur.getString(titleColumn) + "Path: " + cur.getString(filePathIndex));
                MediaFileInfo audio = new MediaFileInfo();
                audio.setFileName(cur.getString(titleColumn));
                audio.setFilePath(cur.getString(filePathIndex));
                audio.setFileType(type);
                mediaList.add(audio);

            } while (cur.moveToNext());
        }

    } catch (Exception e) {
        e.printStackTrace();
    }

}

like image 28
Mukesh Y Avatar answered Oct 08 '22 22:10

Mukesh Y