添加MP3到ContentResolver的 [英] Adding mp3 to the ContentResolver

查看:176
本文介绍了添加MP3到ContentResolver的的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道,从你的应用程序下载的MP3后,你需要将其添加到 ContentResolver的来看看它的音乐播放器。我这样做有以下code:

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); 

    }

我的问题是,我愿意以避免将 DISPLAY_NAME 艺术家 ALBUM 标题的手。

有没有办法告诉机器人从文件中做呢?我已经使用刚 values​​.put(Media.DATA,pathToFile); ,但它没有将它添加到播放

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.

有没有办法迫使它扫描SD音乐的主题?

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

推荐答案

首先尝试添加这样的:

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(); 
}

添加一个mp3后,只要调用此方法,需要的文件名作为输入。

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

,并确保您的活动的名称替换THE_NAME_OF_YOUR_ACTIVITY。

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

这篇关于添加MP3到ContentResolver的的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆