MediaScannerConnectionClient,增加多媒体内容进入系统库

列子:

import com.teleca.jamendo.JamendoApplication;
import com.teleca.jamendo.service.DownloadService;

import android.media.MediaScannerConnection;
import android.media.MediaScannerConnection.MediaScannerConnectionClient;
import android.net.Uri;
import android.util.Log;

/**
 * Forces mp3 file scan on a downloaded file and adds it to the Android MusicPlayer's
 * library
 *
 * @author Lukasz Wisniewski
 */
public class MediaScannerNotifier implements MediaScannerConnectionClient{
    
    private MediaScannerConnection mConnection;
    private DownloadJob mDownloadJob;
    private DownloadService mService;
    static private int mScannedFilesInProgress = 0;

    public MediaScannerNotifier(DownloadService service, DownloadJob job) {
        mDownloadJob = job;
        mService = service;
        mConnection = new MediaScannerConnection(mService, this);
        mConnection.connect();
    }

    @Override
    public void onMediaScannerConnected() {
        //String path = mDownloadJob.getDestination();
        String path = DownloadHelper.getAbsolutePath(mDownloadJob.getPlaylistEntry(), mDownloadJob.getDestination());
        String fileName = DownloadHelper.getFileName(mDownloadJob.getPlaylistEntry(), mDownloadJob.getFormat());
        Log.i(JamendoApplication.TAG, "Adding to media library -> "+fileName);        
        if(mConnection.isConnected())
        {
            mConnection.scanFile(path+"/"+fileName, null);
            mScannedFilesInProgress++;            
        }
        

    }

    @Override
    public void onScanCompleted(String text, Uri uri) {
        Log.i(JamendoApplication.TAG, "Added to media library -> "+uri.toString());
        
        mScannedFilesInProgress--;
        mConnection.disconnect();
        
        // stop service if there is no downloads left
        if(mScannedFilesInProgress == 0){
            mService.notifyScanCompleted();
        }
    }

}


你可能感兴趣的:(Android)