get song from folder android - android

I'm using code below to get all songs from folder, but it's not working because it will only work with such paths: /sdcard/test.mp3, but i need to get songs from folder- eg /sdcard/
How can i do this without getting all songs?
public Playlist getSongsByPath(String name){
String where = MediaStore.Audio.Media.DATA
+ "=?";
String whereVal[] = { name };
String orderBy = MediaStore.Audio.Media.TITLE;
Uri media = MediaStore.Audio.Media.EXTERNAL_CONTENT_URI;
ContentResolver resolver = mContext.getContentResolver();
Cursor cursor = resolver.query(media, Song.FILLED_PROJECTION, where, whereVal, orderBy);
Playlist result = new Playlist();
while(cursor.moveToNext()){
result.addSong(new Song(cursor));
}
return result;
}

By using the Mediastore, you will find all music on your device. The media scanning process maintains the android music database. The _DATA attribute holds the fully qualified pathname so could have differing paths. Eg /mnt/sdcard, /sdcard or wherever you have placed your music etc.
If you want to search the filesystem then mediastore is probably not the correct way to do it.
Ps. Your question is a little confusing? Perhaps you mean something else than "get songs from folder eg /sdcard/"

Related

Is there any way to fetch song's genre using MediaStore?

Using this method of audio file retrieval from Android's external storage
Cursor cursor = getContentResolver().query(MediaStore.Audio.Media.EXTERNAL_CONTENT_URI, null, null, null, null);
can I actually find a resonable way to fetch a genre of the given song? MediaStore class seems to provide everything else - from song's title to its composer info - except for the genre field. Should I use MediaMetadataRetriever then? If so, how drastically can creating a MediaMetadataRetriever instance for every song on a device reduce app's performance?
Maybe there are some better ways to retrieve all audio files from both external and internal storages in android?
As mentioned at Developer's Site,
You can fetch the Genres of the Audio file using MediaStore.Audio.Genres
Sample Code :
private static String[] genresProj = {
MediaStore.Audio.Genres.NAME,
MediaStore.Audio.Genres._ID
};
int idIndex = cursor
.getColumnIndexOrThrow(MediaStore.Audio.Media._ID);
while (cursor.moveToNext()){
int id = Integer.parseInt(mediaCursor.getString(idIndex));
Uri uri = MediaStore.Audio.Genres.getContentUriForAudioId("external", id );
genresCursor = context.getContentResolver().query(uri,
genresProj , null, null, null);
int genreIndex = genresCursor.getColumnIndexOrThrow(MediaStore.Audio.Genres.NAME);
while (genresCursor.moveToNext()) {
Log.d(TAG, "Genre = " +genresCursor.getString(genreIndex));
}
}
}
To fetch other details of the Audio file, please check here .

MediaStore sort by Folder with CursorLoader

I understand how to query "music files" from MediaStore with cursor loader.
I also do not see problem to query specific Album, Genre, Artist for songs. But I have no idea how to query MediaStore for song according to the directory in which they are located.
final Uri sourceUri = MediaStore.Audio.Media.EXTERNAL_CONTENT_URI;
CursorLoader cursorLoader = new CursorLoader(
context,
sourceUri,
null,
null,
null,
MediaStore.Audio.Media.TITLE);
Also if you can help me to get number of songs in album, genre, etc. in same query it would be great.
Thank you for any help in advance.
Michal, your question and example are confusing. You ask about music, yet your code is about Images.
Anyway, to get the song directory
Uri uri = MediaStore.Audio.Media.EXTERNAL_CONTENT_URI;
final String path = MediaStore.Audio.Media.DATA;
so when you have a cursor say c which returns _DATA,
String strpath = (c.getString(c.getColumnIndex(MediaStore.Audio.Media._DATA)));
this will return the full path including the song title itself.
To get number of tracks of an album (where c is the cursor returned)
String strnosongs = (c.getString(c
.getColumnIndex(MediaStore.Audio.Albums.NUMBER_OF_SONGS)));
I hope this helps.
For current API (18) there is no way to do it with in-build tools. This requires to build something (like own database) to help with this query or use other approach (like use "file browser").

Android ContentResolver.query always returns same data

I have videoplayer app with filebrowser listing all videos on SD card
Code inspired by i want get audio files in sd card
Using ContentResolver, works as expected, but it does not update if the files on card change. I do not mean automatically, but after view/app restart. Not even reinstalling the application helped, still shows the same files. The deleted video file is not visible via PC nor it is possible to play it (This video cannot be played (translation)).
I dumped the data and the problem is not in view caching or elsewhere. I do not implement any caching of my own and failed to find anything on the matter. Thank you
Code:
// acquisition
String[] projection = {
MediaStore.Video.Media._ID,
MediaStore.Video.Media.DISPLAY_NAME,
MediaStore.Video.Media.DURATION,
MediaStore.Video.Media.DATA
};
ContentResolver resolver = getActivity().getContentResolver();
Cursor videoCursor = resolver.query(
MediaStore.Video.Media.EXTERNAL_CONTENT_URI,
projection,
null,
null,
null
);
// extraction
while(cursor.moveToNext()) {
cursorIndex = cursor.getColumnIndexOrThrow(MediaStore.Video.Media.DATA);
filepath = cursor.getString(cursorIndex);
cursorIndex = cursor.getColumnIndexOrThrow(MediaStore.Video.Media.DISPLAY_NAME);
filename = cursor.getString(cursorIndex);
cursorIndex = cursor.getColumnIndexOrThrow(MediaStore.Video.Media.DURATION);
duration = cursor.getString(cursorIndex);
result[ index++ ] = new VideoFileMetadata(filename, duration, filepath);
}
Edit 1 [14-03-2013]:
I tried adding number + " = " + number to ORDER or WHERE clause to act as a potential query caching buster, but it had no effect (although it's possible it was removed by an optimizer as a useless clause). This time I had reinstalled the application from a different machine using different certificate, but the query result remained the same, listing currently non-existing files.
You should first call cursor.moveToFirst() .
So, your cursor iteration loop should look like
if (cursor.moveToFirst()) {
do {
// cursorIndex = cursor.getColumnIndexOrThrow, etc...
} while (cursor.moveToNext());
}

MediaStore - Uri to query all types of files (media and non-media)

In the class MediaStore.Files class, its mentioned that,
Media provider table containing an index of all files in the media storage, including non-media files.
I'm interested in querying for non-media files like PDF.
I'm using CursorLoader to query the database. The second parameter for the constructor requires an Uri argument which is easy to get for the media types Audio, Images and Video as each of them have a EXTERNAL_CONTENT_URI and INTERNAL_CONTENT_URI constant defined for them.
For MediaStore.Files there is no such defined constant. I tried using the getContentUri() method but couldn't figure out the argument value for volumeName. I tried giving "/mnt/sdcard" and also the volume name that appears when I connect the device to my system but in vain.
I saw a similar question on Google Groups but that is not resolved.
EDIT: I also tried using Uri.fromFile(new File("/mnt/sdcard/")) and Uri.parse(new File("/mnt/sdcard").toString()) but that didn't work out either.
It is "external" or "internal" although internal (system files) is probably not useful here.
ContentResolver cr = context.getContentResolver();
Uri uri = MediaStore.Files.getContentUri("external");
// every column, although that is huge waste, you probably need
// BaseColumns.DATA (the path) only.
String[] projection = null;
// exclude media files, they would be here also.
String selection = MediaStore.Files.FileColumns.MEDIA_TYPE + "="
+ MediaStore.Files.FileColumns.MEDIA_TYPE_NONE;
String[] selectionArgs = null; // there is no ? in selection so null here
String sortOrder = null; // unordered
Cursor allNonMediaFiles = cr.query(uri, projection, selection, selectionArgs, sortOrder);
If you want .pdf only you could check the mimetype
// only pdf
String selectionMimeType = MediaStore.Files.FileColumns.MIME_TYPE + "=?";
String mimeType = MimeTypeMap.getSingleton().getMimeTypeFromExtension("pdf");
String[] selectionArgsPdf = new String[]{ mimeType };
Cursor allPdfFiles = cr.query(uri, projection, selectionMimeType, selectionArgsPdf, sortOrder);

How do you access the name, artist, album name off the music stored on the phone?

Much like the music app does I want to access the name of the song (not the name of the file) or the artist or album. For example, I want to populate a listview with the names of all the songs on the phone.
There are two approaches to this, one to read the meta tags from the music files themselves and one to use the MediaStore content provider. MediaStore is essentially a public database that you may query for all media related information on the phone.
Using MediaStore is very simple and can be found in the docs here.
Here is a simple example from one of my applications:
String[] proj = { MediaStore.Audio.Media._ID,
MediaStore.Audio.Media.DATA,
MediaStore.Audio.Media.DISPLAY_NAME,
MediaStore.Audio.Artists.ARTIST };
tempCursor = managedQuery(MediaStore.Audio.Media.EXTERNAL_CONTENT_URI,
proj, null, null, null);
tempCursor.moveToFirst(); //reset the cursor
int col_index=-1;
int numSongs=tempCursor.getCount();
int currentNum=0;
do{
col_index = tempCursor.getColumnIndexOrThrow(MediaStore.Audio.Artists.ARTIST);
artist = tempCursor.getString(col_index);
//do something with artist name here
//we can also move into different columns to fetch the other values
}
currentNum++;
}while(tempCursor.moveToNext());

Categories

Resources