【问题标题】:Android: Ringtone is being saved, but not set as the Active/Default ringtoneAndroid:正在保存铃声,但未设置为活动/默认铃声
【发布时间】:2011-03-25 04:33:16
【问题描述】:

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

【问题讨论】:

    标签: java android ringtone


    【解决方案1】:

    不确定你是否发现了这个,但我最近才发现。将您的插入数据库行替换为:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多