tags:

views:

75

answers:

1

I know that after downloading an mp3 from your app you need to add it to the ContentResolver to see it on the music player. I am doing it with the following code:

    private void addFileToContentProvider() {
        ContentValues values = new ContentValues(7);

        values.put(Media.DISPLAY_NAME, "display_name");
        values.put(Media.ARTIST, "artist");
        values.put(Media.ALBUM, "album");
        values.put(Media.TITLE, "Title"); 
        values.put(Media.MIME_TYPE, "audio/mp3"); 
        values.put(Media.IS_MUSIC, true);
        values.put(Media.DATA, pathToFile);

        context.getContentResolver().insert(Media.EXTERNAL_CONTENT_URI, values); 

    }

My issue is that I am willing to avoid setting DISPLAY_NAME, ARTIST, ALBUM, TITLE by hand.

Is there a way to tell Android to do it from the file? I've already used just values.put(Media.DATA, pathToFile); but it's not adding it to the player.

Is there a way to force the thread that scans the sd for music?

+1  A: 

Hi Macarse,

First try adding this:

values.put(MediaStore.Audio.Media.IS_MUSIC, true);

Then try this:

sendBroadcast(new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE, Uri.parse("file://"+path+filename)));

If that doesn't work, try this:

public MediaScannerConnection mScanner = null;

public void reScan(String filename){
    final String name = filename;
    mScanner = new MediaScannerConnection(THE_NAME_OF_YOUR_ACTIVITY.this, new MediaScannerConnection.MediaScannerConnectionClient() {
        public void onMediaScannerConnected() { 
            mScanner.scanFile(name, null); 
        } 
        public void onScanCompleted(String path, Uri uri) {
            if (path.equals(name)) {
                mScanner.disconnect(); 
            } 
        }
    }); 
    mScanner.connect(); 
}

After adding an mp3, just call this method which takes the file name as the input.

And make sure to replace THE_NAME_OF_YOUR_ACTIVITY with the name of your activity.

magicman
What is: `values.put(MediaStore.Audio.Media.IS_MUSIC, false);` for?
Macarse
sorry, I wrote it wrong. It should be true. Its just saying that its a music file to be played on the music player.
magicman
@magicman: I've added your code but `onMediaScannerConnected` is never called. Any ideas?
Macarse
It should work now. I edited my code and added the following: public MediaScannerConnection mScanner = null;
magicman
@magicman: I already added that. Somehow it's not working :(
Macarse
actually I think this may be much simpler: sendBroadcast(new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE, Uri.parse("file://"+path+filename)));
magicman
Also, the IS_MUSIC is just a flag for filtering media. There are other flags like IS_RINGTONE, IS_ALARM, etc so that you can query for just the media in which you are interested.
dhaag23
`Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE, Uri.parse("file://"+path+filename)));` this last line worked!
Macarse