i want get audio files in sd card - android

In my app I want to set the ringtone when I get an incoming call... How to open the SDCARD and get audio files and list it.. How to get the URI for the selected audio file..

MediaScanner finds music for you, populating the MediaStore database. Here's some code to look up a music entry:
final Uri uri = MediaStore.Audio.Media.EXTERNAL_CONTENT_URI;
final String[] cursor_cols = {
MediaStore.Audio.Media._ID,
MediaStore.Audio.Media.ARTIST,
MediaStore.Audio.Media.ALBUM,
MediaStore.Audio.Media.TITLE,
};
final String where = MediaStore.Audio.Media.IS_MUSIC + "=1";
final Cursor cursor = getContentResolver().query(uri, cursor_cols, where, null, null);
cursor.moveToNext();
final String artist = cursor.getString(_cursor.getColumnIndexOrThrow(MediaStore.Audio.Media.ARTIST));
final String album = cursor.getString(_cursor.getColumnIndexOrThrow(MediaStore.Audio.Media.ALBUM));
final String track = cursor.getString(_cursor.getColumnIndexOrThrow(MediaStore.Audio.Media.TITLE));
doSomethingHere(artist, album, track);
The "data" field contains a handle you can use with MediaPlayer. while doing this media files scanning and indexing for the first time, scan it using a AsyncTask, but later when a intent is received do it using a service.

Related

Playing MediaStore.Audio.Media Entries

I'm writing an app that can play all audio files present in MediaStore.Audio.Media.EXTERNAL_CONTENT_URI.
Here's a bit of what I have so far:
String[] projection = {MediaStore.Audio.Media.ARTIST,
MediaStore.Audio.Media.DISPLAY_NAME,
MediaStore.Audio.Media.ALBUM,
MediaStore.Audio.Media.DATA};
Cursor cursor = contentResolver.query(MediaStore.Audio.Media.EXTERNAL_CONTENT_URI,
projection,
null,
null,
null);
while (cursor.moveToNext()) {
String uri = cursor.getString(cursor.getColumnIndex(MediaStore.Audio.Media.DATA));
// Use uri to play music...
}
And this works fine. I'm able to play the audio by passing the uri to a MediaPlayer.
However...
MediaStore.Audio.Media.DATA has no guarantees of holding a uri. To my understanding, it can hold absolutely anything.
MediaStore.Audio.Media.DATA is deprecated.
Is there any way to play the audio items in external storage, without relying on MediaStore.Audio.Media.DATA?
Yes, it is.
You can simply play it with the ContentUris.withAppendedId, as MediaPlayer has a method for playing content by Uri.
Here, for example, i'm setting the song Uri.
/// Helper function
public static Uri getSongUri(int songId) {
return ContentUris.withAppendedId(MediaStore.Audio.Media.EXTERNAL_CONTENT_URI, songId);
}
/// And then...
public void setUri(Context appContext, int songId) {
// ....
player.setDataSource(appContext, getSongUri(songId));
// ....
}

How to filter ringtone, alarm and relevant media files in android

I am trying to retrieve music file from the android INTERNAL STORAGE URI but I am gettting the alarm and ringtones also I came to know about filtering from here .So i tried to set the duration equal to 30sec then also I am getting some alarm file .I can not set multiple condition this is what I tried
String[] proj2 = {MediaStore.Audio.Media._ID, MediaStore.Audio.Media.TITLE};// Can include more data for more details and check it.
String selection = MediaStore.Audio.Media.DURATION + ">=30000";
Cursor audioCursor = getContentResolver().query(MediaStore.Audio.Media.INTERNAL_CONTENT_URI, proj2, selection,null, null);
I tried to select by ringtone and is_music but then also i am getting some alarm files .So what should i try to filter all the ringtones ,alarm and other such inbuilt files
Hei man , I look up your code . Once I have faced the same problem while doing media Query with Cursor audioCursor = getContentResolver().query(MediaStore.Audio.Media.INTERNAL_CONTENT_URI, proj2, selection,null, null);
but Everything you did is perfect You just need to Query through Uri musicUri = android.provider.MediaStore.Audio.Media.EXTERNAL_CONTENT_URI;
so your whole content resolver and query should be like this
String[] proj2 = {MediaStore.Audio.Media._ID, MediaStore.Audio.Media.TITLE};// Can include more data for more details and check it.
String selection = MediaStore.Audio.Media.DURATION + ">=30000";
Cursor audioCursor = getContentResolver().query(android.provider.MediaStore.Audio.Media.EXTERNAL_CONTENT_URI, proj2, selection,null, null);

get song from folder 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/"

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 to get track Uri from ContentResolver query?

i am traversing in the phone's memory with the following code.
ContentResolver contentResolver = getContentResolver();
String[] columns = {
MediaColumns._ID,
MediaColumns.TITLE,
AudioColumns.DURATION,
};
final String where = MediaStore.Audio.Media.IS_MUSIC + "=1";
Cursor cursor = contentResolver.query(MediaStore.Audio.Media.EXTERNAL_CONTENT_URI,columns, where, null, null);
while(cursor.moveToNext()) {
String urp = cursor.getString(cursor.getColumnIndex(MediaColumns._ID));
String title = cursor.getString(cursor.getColumnIndex(MediaColumns.TITLE));
Long duration = cursor.getLong(cursor.getColumnIndex(AudioColumns.DURATION));
}
so now my question is, how to get the uri of the cursor items traveresed, like
for first while run i have stored id,title,duration etc metadata and i would also like to
store that tracks URI, also i would like to convert that URI from content// scheme to file scheme.
Any help will be greatful
thankyou
String.valueOf(cur.getColumnIndex(android.provider.MediaStore.MediaColumns.DATA)));
http://android-er.blogspot.fr/2011/04/convert-uri-to-real-path-format.html

Categories

Resources