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

发布于 2024-10-26 07:45:39 字数 2200 浏览 8 评论 0原文

我正在开发一个 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;  
     }   

I'm working on an Android application that allows users to long-click on a button to save a sound as a ringtone. I am using the code below to do so. The code currently works to save the file in the list of ringtones to be used, however it does not automatically set the sound as the default ringtone. I have searched all around and not had much luck finding a clear guide on saving a sound as the default/active ringtone.

As of now, the user can long-click the button, then go into the Menu > Sounds > Phone Ringtone menu and select from the list, but that seems a bit inconvenient when I know that it is possible to have it simply set as the active ringtone straight away.

Any insight as to what I am missing? Much appreciated!

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;  
     }   

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

凉月流沐 2024-11-02 07:45:39

不确定你是否明白了这一点,但我最近才明白。将插入数据库行替换为:

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
            );

Not sure if you figured this one out, but I just did recently. Replace your Insert Database line with this:

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
            );
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文