Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/199.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java Android:铃声正在保存,但未设置为活动/默认铃声_Java_Android_Ringtone - Fatal编程技术网

Java Android:铃声正在保存,但未设置为活动/默认铃声

Java Android:铃声正在保存,但未设置为活动/默认铃声,java,android,ringtone,Java,Android,Ringtone,我正在开发一个Android应用程序,它允许用户长时间点击按钮,将声音保存为铃声。我正在使用下面的代码来执行此操作。该代码当前用于将文件保存在要使用的铃声列表中,但不会自动将声音设置为默认铃声。我已经到处搜索过了,但没有找到关于将声音保存为默认/活动铃声的清晰指南 到目前为止,用户可以长时间单击按钮,然后进入菜单>声音>手机铃声菜单并从列表中选择,但当我知道可以直接将其设置为活动铃声时,这似乎有点不方便 对我遗漏的东西有什么见解吗?非常感谢 public boolean saveas(int r

我正在开发一个Android应用程序,它允许用户长时间点击按钮,将声音保存为铃声。我正在使用下面的代码来执行此操作。该代码当前用于将文件保存在要使用的铃声列表中,但不会自动将声音设置为默认铃声。我已经到处搜索过了,但没有找到关于将声音保存为默认/活动铃声的清晰指南

到目前为止,用户可以长时间单击按钮,然后进入菜单>声音>手机铃声菜单并从列表中选择,但当我知道可以直接将其设置为活动铃声时,这似乎有点不方便

对我遗漏的东西有什么见解吗?非常感谢

public boolean saveas(int ressound){  
      byte[] buffer=null;  
      InputStream fIn = getBaseContext().getResources().openRawResource(ressound);  
      int size=0;  

      try {  
       size = fIn.available();  
       buffer = new byte[size];  
       fIn.read(buffer);
       fIn.close();  
      } catch (IOException e) {  
       // TODO Auto-generated catch block  
       return false;  
      }  

      String path="/sdcard/media/audio/ringtones/";  
      String filename="ADTone"+".ogg";  

      boolean exists = (new File(path)).exists();  
      if (!exists){new File(path).mkdirs();}  

      FileOutputStream save;  
      try {  
       save = new FileOutputStream(path+filename);  
       save.write(buffer);  
       save.flush();  
       save.close();  
      } catch (FileNotFoundException e) {  
       // TODO Auto-generated catch block  
       return false;  
      } catch (IOException e) {  
       // TODO Auto-generated catch block  
       return false;  
      }      

      sendBroadcast(new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE, Uri.parse("file://"+path+filename)));  

      File k = new File(path, filename);  

      ContentValues values = new ContentValues();  
      values.put(MediaStore.MediaColumns.DATA, k.getAbsolutePath());  
      values.put(MediaStore.MediaColumns.TITLE, "AD Ringtone");  
      values.put(MediaStore.MediaColumns.MIME_TYPE, "audio/ogg");  
      values.put(MediaStore.Audio.Media.ARTIST, "adtone ");  
      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);  

      //Insert it into the database  
      this.getContentResolver().insert(MediaStore.Audio.Media.getContentUriForPath(k.getAbsolutePath()), values);  


      return true;  
     }   

我不知道你是否知道这一点,但我最近才知道。将插入数据库行替换为以下内容:

Uri uri = MediaStore.Audio.Media.getContentUriForPath(k.getAbsolutePath());

            getContentResolver().delete(uri, MediaStore.MediaColumns.DATA + "=\"" + k.getAbsolutePath() + "\"", null);

            Uri newUri = getContentResolver().insert(uri, values);

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