尝试设置默认铃声会获得SecurityException

时间:2012-12-19 23:53:52

标签: android

我正在尝试使用它:

RingtoneManager.setActualDefaultRingtoneUri(this, RingtoneManager.TYPE_RINGTONE, newUri);

...设置默认铃声。类似SecurtyException的例外情况就会消失。

我看了看这个:

RingtoneManager.setActualDefaultRingtoneUri(this, RingtoneManager.TYPE_RINGTONE, newUri);

...看看是否有任何权限可以在menefest文件中设置与铃声相对应的内容,找不到任何内容。

以下是我的代码:

// make it a ring tone
    void   MakeRingtune( String name)
    {

    File newSoundFile = new File("/sdcard/", "myringtone.oog");

    String strUri = "android.resource://"+getPackageName()+  "/" + "raw/"+name;
    Uri mUri = Uri.parse(strUri);

    ContentResolver mCr = getContentResolver();
    AssetFileDescriptor soundFile;
    try {
           soundFile= mCr.openAssetFileDescriptor(mUri, "r");
       } catch (FileNotFoundException e) {
           MessageBox("Ringtone Manager ","System Error cannot add ringtone ");
           return;  
       }

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

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

          fos.close();
       } catch (IOException io) {
           MessageBox("Ringtone Manager ","Could not copy Ringtone, may be due to no sd card");
           return;
       }

//////////////////////////////////////////
       ContentValues values = new ContentValues();
       values.put(MediaStore.MediaColumns.DATA, newSoundFile.getAbsolutePath());
       values.put(MediaStore.MediaColumns.TITLE, "my ringtone");
       values.put(MediaStore.MediaColumns.MIME_TYPE, "audio/oog");
       values.put(MediaStore.MediaColumns.SIZE, newSoundFile.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, false);

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


       try {
           RingtoneManager.setActualDefaultRingtoneUri(this, RingtoneManager.TYPE_RINGTONE, newUri);
       } catch (Throwable t) {
        //   Log.d(TAG, "catch exception");
           MessageBox("Ringtone Manager ","Could not set as your default ringtone ");
           return;
       }

///////////////////////////////////////   
       MessageBox("Ringtone Manager ","Sound Clip Added to your Ringtones");
    } // end methed

1 个答案:

答案 0 :(得分:1)

“android.permission.WRITE_SETTINGS”是您需要的。