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

}


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章