2014-03-27 44 views

回答

0

答案...

Set ringtone from res/raw folder

不能從資源設置的鈴聲。

RingtoneManager.setActualDefaultRingtoneUri需要設備中的鈴聲文件,並且Uri應該來自內容解析器。

File ring = new File("pathOfYourFile"); 
    Uri path = MediaStore.Audio.Media.getContentUriForPath(ring.getAbsolutePath()); 

RingtoneManager.setActualDefaultRingtoneUri(getApplicationContext(),RingtoneManager.TYPE_RINGTONE,路徑);

同時一定要添加權限

0

試試這個代碼

String name = "your_raw_audio_name"; 

File file = new File(Environment.getExternalStorageDirectory(), 
      "/myRingtonFolder/Audio/"); 
    if (!file.exists()) { 
     file.mkdirs(); 
    } 

    String path = Environment.getExternalStorageDirectory() 
      .getAbsolutePath() + "/myRingtonFolder/Audio/"; 

    File f = new File(path + "/", name + ".mp3"); 

    Uri mUri = Uri.parse("android.resource://" 
      + context.getPackageName() + "/raw/" + name); 
    ContentResolver mCr = context.getContentResolver(); 
    AssetFileDescriptor soundFile; 
    try { 
     soundFile = mCr.openAssetFileDescriptor(mUri, "r"); 
    } catch (FileNotFoundException e) { 
     soundFile = null; 
    } 

    try { 
     byte[] readData = new byte[1024]; 
     FileInputStream fis = soundFile.createInputStream(); 
     FileOutputStream fos = new FileOutputStream(f); 
     int i = fis.read(readData); 

     while (i != -1) { 
      fos.write(readData, 0, i); 
      i = fis.read(readData); 
     } 

     fos.close(); 
    } catch (IOException io) { 
    } 
    ContentValues values = new ContentValues(); 
    values.put(MediaStore.MediaColumns.DATA, f.getAbsolutePath()); 
    values.put(MediaStore.MediaColumns.TITLE, name); 
    values.put(MediaStore.MediaColumns.MIME_TYPE, "audio/mp3"); 
    values.put(MediaStore.MediaColumns.SIZE, f.length()); 
    values.put(MediaStore.Audio.Media.ARTIST, R.string.app_name); 
    values.put(MediaStore.Audio.Media.IS_RINGTONE, true); 
    values.put(MediaStore.Audio.Media.IS_NOTIFICATION, true); 
    values.put(MediaStore.Audio.Media.IS_ALARM, true); 
    values.put(MediaStore.Audio.Media.IS_MUSIC, true); 

    Uri uri = MediaStore.Audio.Media.getContentUriForPath(f 
      .getAbsolutePath()); 
    Uri newUri = mCr.insert(uri, values); 

    try { 
     RingtoneManager.setActualDefaultRingtoneUri(context, 
       RingtoneManager.TYPE_RINGTONE, newUri); 
     Settings.System.putString(mCr, Settings.System.RINGTONE, 
       newUri.toString()); 
    } catch (Throwable t) { 

    } 
的更多細節

檢查這個link

+0

不能正常工作.... – user3437592

相關問題