试图设置默认铃声得到一个SecurityException [英] Trying to set default ringtone get a SecurityException

查看:97
本文介绍了试图设置默认铃声得到一个SecurityException的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图用这个:

  RingtoneManager.setActualDefaultRingtoneUri(这一点,RingtoneManager.TYPE_RINGTONE,newUri);

...设置默认铃声。无一例外熄灭,以键入 SecurtyException

我看着这样的:

  RingtoneManager.setActualDefaultRingtoneUri(这一点,RingtoneManager.TYPE_RINGTONE,newUri);

......,看看是否有任何权限对应的是铃声menefest文件来设置,也没有找到。

以下是我的code:

  //使它成为铃声
    无效MakeRingtune(字符串名称)
    {    文件newSoundFile =新的文件(/ SD卡/,​​myringtone.oog);    串strUri =android.resource://+ getPackageName()+/+的生/+名称;
    乌里穆里= Uri.parse(strUri);    ContentResolver的MCR = getContentResolver();
    AssetFileDescriptor音效档;
    尝试{
           音效档= mCr.openAssetFileDescriptor(穆里,R);
       }赶上(FileNotFoundException异常五){
           的MessageBox(铃声管理器,系统错误不能添加铃声);
           返回;
       }       尝试{
          字节[] = READDATA新的字节[1024];
          FIS的FileInputStream = soundFile.createInputStream();
          FOS的FileOutputStream =新的FileOutputStream(newSoundFile);
          INT I = fis.read(READDATA);          而(ⅰ!= -1){
            fos.write(READDATA,0,I);
            I = fis.read(READDATA);
          }          fos.close();
       }赶上(IOException异常IO){
           的MessageBox(铃声管理器,无法复制铃声,可能是由于无SD卡);
           返回;
       }//////////////////////////////////////////
       ContentValues​​值=新ContentValues​​();
       values​​.put(MediaStore.MediaColumns.DATA,newSoundFile.getAbsolutePath());
       values​​.put(MediaStore.MediaColumns.TITLE,我的手机铃声);
       values​​.put(MediaStore.MediaColumns.MIME_TYPE,音频/ 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,真);
       values​​.put(MediaStore.Audio.Media.IS_NOTIFICATION,真);
       values​​.put(MediaStore.Audio.Media.IS_ALARM,真);
       values​​.put(MediaStore.Audio.Media.IS_MUSIC,FALSE);       URI的uri = MediaStore.Audio.Media.getContentUriForPath(newSoundFile.getAbsolutePath());
       乌里newUri = mCr.insert(URI,价值观);
       尝试{
           RingtoneManager.setActualDefaultRingtoneUri(这一点,RingtoneManager.TYPE_RINGTONE,newUri);
       }赶上(的Throwable t)的{
        // Log.d(TAG,catch异常);
           的MessageBox(铃声管理器,无法设置为默认铃声);
           返回;
       }///////////////////////////////////////
       的MessageBox(铃声经理,声音剪辑添加到您的手机铃声);
    } //结束基法


解决方案

android.permission.WRITE_SETTINGS是你需要具备什么。

I'm trying to use this:

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

... to set the default ringtone. A exception goes off, with type SecurtyException.

I looked at this:

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

... to see if there was any permissions to set in menefest file that corresponded to ringtones, could not find any.

The following is my code:

// 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

解决方案

"android.permission.WRITE_SETTINGS" is what you need to have.

这篇关于试图设置默认铃声得到一个SecurityException的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆