Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲
 Android教程網 >> Android技術 >> 關於Android編程 >> Android FileManager 中點擊音樂文件設置為來電鈴聲 M

Android FileManager 中點擊音樂文件設置為來電鈴聲 M

編輯:關於Android編程

 

正文

 

 

Step1: AndroidManifest.xml
Add use permission


Step2: resource
resmenupopup_menu.xml


resaluesstrings.xml
Use As Ringtone
Use As Ringtone
多國語言需要添加在對應的values_xxxxstrings.xml  中
Step3:import
FileManagerOperationActivity.java
import android.provider.MediaStore;
import android.database.Cursor;
import android.content.ContentResolver;
import android.content.ContentValues;
import android.provider.Settings;
import android.widget.Toast;
import android.content.ContentUris;
import android.media.MediaFile;

Step4:顯示、隱藏setRingtone菜單 
updateEditPopupMenu() 方法改為如下
        private void updateEditPopupMenu() {
            final Menu menu = mEditPopupMenu.getMenu();
            int selectedCount = mAdapter.getCheckedItemsCount();
            // remove (disable) protection info icon
            menu.removeItem(R.id.protection_info);
            if (selectedCount == 0) {
                menu.findItem(R.id.rename).setEnabled(false);
                menu.findItem(R.id.details).setEnabled(false);
                //add
                menu.findItem(R.id.set_ringtone).setEnabled(false);
            } else if (selectedCount == 1) {
                // enable details icon
                menu.findItem(R.id.details).setEnabled(true);
                // enable rename icon
                if (mAdapter.getCheckedFileInfoItemsList().get(0).getFile().canWrite()) {
                    menu.findItem(R.id.rename).setEnabled(true);
                }
                //add
                // only audio file can enable set ringtone
                FileInfo fileInfo1 = mAdapter.getCheckedFileInfoItemsList().get(0);
                if(!fileInfo1.isDrmFile() 
                        && !fileInfo1.isDirectory()
                        && IsMusic(fileInfo1)){
                       LogUtils.d(TAG, need set ringtone );
                    menu.findItem(R.id.set_ringtone).setEnabled(true);
                }else {
                    menu.findItem(R.id.set_ringtone).setEnabled(false);
                }                
                
                
                // enable protection info icon
                FileInfo fileInfo = mAdapter.getCheckedFileInfoItemsList().get(0);
                if (fileInfo.isDrmFile()) {
                    String path = fileInfo.getFileAbsolutePath();
                    if (DrmManager.getInstance().checkDrmObjectType(path)) {
                        String mimeType = DrmManager.getInstance().getOriginalMimeType(path);
                        if (mimeType != null && mimeType.trim().length() != 0) {
                            menu.add(0, R.id.protection_info, 0,
                                    com.mediatek.internal.R.string.drm_protectioninfo_title);
                        }
                    }
                }
            } else {
                // disable details icon
                menu.findItem(R.id.details).setEnabled(false);
                // disable rename icon
                menu.findItem(R.id.rename).setEnabled(false);
                //add
                menu.findItem(R.id.set_ringtone).setEnabled(false);
            }
        }
            


Step5:  .判斷選擇文件是否是audio 文件
定義IsMusic 方法
private boolean IsMusic(FileInfo fileinfo ){
            String mimeType = fileinfo.getFileMimeType(mService);
            
    int fileType = MediaFile.getFileTypeForMimeType(mimeType);
    if (mimeType.startsWith(audio/) || MediaFile.isAudioFileType(fileType)) {
        return true;
    }else {
        return false;
    }
}

Step6:  定義設置ringtone 方法
新增方法    setRingtone
    public void setRingtone(MenuItem menuItem){
            LogUtils.d(TAG, start setRingtone);
            FileInfo fileinfo = mAdapter.getCheckedFileInfoItemsList().get(0);
            
            ContentResolver resolver = getApplicationContext().getContentResolver();
            String filepath = fileinfo.getFileAbsolutePath();
            Long fileid = 0L;
            //Uri fileuri=Uri fileuri= getUriForPath(filepath);
            Uri fileuri= MediaStore.Audio.Media.getContentUriForPath(filepath);
            
            LogUtils.d(TAG, setRingtone uri+fileuri);
            LogUtils.d(TAG, setRingtone path+filepath);
            Cursor file_cursor = resolver.query(
                fileuri,
                null,
                MediaStore.MediaColumns.DATA+=?,
                new String[]{filepath},
                null);
            if(file_cursor.moveToFirst() && file_cursor.getCount() >0){
                String _id = file_cursor.getString(0);
                fileid =  Long.valueOf(_id);
            }
            Uri ringUri = ContentUris.withAppendedId(MediaStore.Audio.Media.EXTERNAL_CONTENT_URI,
                fileid);
            try {
                ContentValues values = new ContentValues(2);
                values.put(MediaStore.Audio.Media.IS_RINGTONE, 1);
                values.put(MediaStore.Audio.Media.IS_ALARM, 1);
                resolver.update(ringUri, values, null, null);
            } catch (UnsupportedOperationException ex) {
                LogUtils.d(TAG, couldn't set ringtone flag for id  + ringUri);
                return;
            }
            
            LogUtils.d(TAG, setRingtone ringUri+ringUri.toString());
            String[] cols = new String[] {
                    MediaStore.Audio.Media._ID,
                    MediaStore.Audio.Media.DATA,
                    MediaStore.Audio.Media.TITLE
            };
            
            /// M: use selectionArgs replace set query value in where @{
            String where = MediaStore.Audio.Media._ID + =?;
            String[] whereArgs = new String[]{String.valueOf(fileid)};
            Cursor cursor = resolver.query(
                MediaStore.Audio.Media.EXTERNAL_CONTENT_URI,
                cols, 
                where ,
                whereArgs,
                null);
            /// @}
            try {
                if (cursor != null && cursor.getCount() == 1) {
                    // Set the system setting to make this the current ringtone
                    cursor.moveToFirst();
                    Settings.System.putString(resolver, Settings.System.RINGTONE, ringUri.toString());
                    Toast.makeText(getApplicationContext(), setRingtone Success, Toast.LENGTH_SHORT).show();
                }
            } finally {
                if (cursor != null) {
                    cursor.close();
                }
            }
    }

Step7:重新定義MediaStore.java getContentUriForPath  方法
之所以定義getContentUriForPath,是因為當時開發該feature的時候,發現getContentUriForPath這個函數有bug,當路徑是/storage/sdcard1/  時候,無法正確返回uri,返回來的竟然是Internal_content_uri。
public static Uri getContentUriForPath(String path) {
                return((path.startsWith(Environment.getRootDirectory()+/media))?
                INTERNAL_CONTENT_URI :EXTERNAL_CONTENT_URI);
            }


Step8:響應setRingtone 
在onMenuItemClick  方法裡面增加
            case R.id.set_ringtone:
                setRingtone(menuItem);
                break;

最後編譯
  ./mk mm ./mediatek/packages/apps/FileManager/


 

  1. 上一頁:
  2. 下一頁:
熱門文章
閱讀排行版
Copyright © Android教程網 All Rights Reserved