【问题标题】:How to set ringtone through an android application?如何通过android应用程序设置铃声?
【发布时间】:2011-04-11 06:19:35
【问题描述】:

我正在尝试通过我的 android 应用程序设置铃声。我试过这段代码

  public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    String filepath ="/sdcard/sound.mp3";
    File ringtoneFile = new File(filepath);

    ContentValues content = new ContentValues();
    content.put(MediaStore.MediaColumns.DATA,ringtoneFile.getAbsolutePath());
    System.out.println(ringtoneFile.getAbsolutePath()+"+++++++++++++++++++++++++");
    content.put(MediaStore.MediaColumns.TITLE, "chinnu");
    content.put(MediaStore.MediaColumns.SIZE, 215454);
    content.put(MediaStore.MediaColumns.MIME_TYPE, "sound.mp3");
    content.put(MediaStore.Audio.Media.ARTIST, "Madonna");
    content.put(MediaStore.Audio.Media.DURATION, 230);
    content.put(MediaStore.Audio.Media.IS_RINGTONE, true);
    content.put(MediaStore.Audio.Media.IS_NOTIFICATION, false);
    content.put(MediaStore.Audio.Media.IS_ALARM, false);
    content.put(MediaStore.Audio.Media.IS_MUSIC, false);


    //Insert it into the database
    String TAG="";
    Log.i(TAG, "the absolute path of the file is :"+
    ringtoneFile.getAbsolutePath());
    Uri uri = MediaStore.Audio.Media.getContentUriForPath(
    ringtoneFile.getAbsolutePath());
    Uri newUri = Context.getContentResolver().insert(uri, content);
    ringtoneUri = newUri; 
   Log.i(TAG,"the ringtone uri is :"+ringtoneUri);
   RingtoneManager.setActualDefaultRingtoneUri(context,
   RingtoneManager.TYPE_RINGTONE,newUri);


}
}

但我收到错误:context 无法解析为类型。这是什么意思?我是android开发的初学者。我还设置了 write_external_storage permission

我还需要做什么才能成功运行此应用程序?
提前致谢

【问题讨论】:

    标签: android audio mediastore


    【解决方案1】:
        String filepath ="/sdcard/Fast_N_Furious_2009.mp3";
        File ringtoneFile = new File(filepath);
    
        ContentValues content = new ContentValues();
        content.put(MediaStore.MediaColumns.DATA,ringtoneFile.getAbsolutePath());
        content.put(MediaStore.MediaColumns.TITLE, "chinnu");
        content.put(MediaStore.MediaColumns.SIZE, 215454);
        content.put(MediaStore.MediaColumns.MIME_TYPE, "audio/*");
        content.put(MediaStore.Audio.Media.ARTIST, "Madonna");
        content.put(MediaStore.Audio.Media.DURATION, 230);
        content.put(MediaStore.Audio.Media.IS_RINGTONE, true);
        content.put(MediaStore.Audio.Media.IS_NOTIFICATION, false);
        content.put(MediaStore.Audio.Media.IS_ALARM, false);
        content.put(MediaStore.Audio.Media.IS_MUSIC, false);
    
    
        //Insert it into the database
        Log.i(TAG, "the absolute path of the file is :"+
        ringtoneFile.getAbsolutePath());
        Uri uri = MediaStore.Audio.Media.getContentUriForPath(ringtoneFile.getAbsolutePath());
        //Uri newUri = context.getContentResolver().insert(uri, content);
        Uri newUri = getContentResolver().insert(uri, content); 
        String ringtoneUri = newUri.toString();
    
        Log.i(TAG,"the ringtone uri is :"+ringtoneUri);
        RingtoneManager.setActualDefaultRingtoneUri(getBaseContext(),RingtoneManager.TYPE_RINGTONE,newUri);
    

    【讨论】:

      【解决方案2】:

      嗨,Sarath,如果您尝试在您的活动中删除代码中的上下文。

      换行

      Uri newUri = getContentResolver().insert(uri, content); ringtoneUri = newUri;

      【讨论】:

      • @milind...谢谢 ....现在它在这 3 行中显示错误..ringtoneUri = newUri; Log.i(TAG,"铃声uri为:"+ringtoneUri); RingtoneManager.setActualDefaultRingtoneUri(context, RingtoneManager.TYPE_RINGTONE,newUri);...... ringtoneuri 没有解析为类型......然后在 RingtoneManager.setActualDefaultRingtoneUri(context, RingtoneManager.TYPE_RINGTONE,newUri);. 在这段代码中我们正在使用上下文......如何避免......?
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2010-11-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-05-29
      相关资源
      最近更新 更多