如何通过Android应用程序设置铃声?

时间:2011-04-11 06:19:36

标签: android audio mediastore

我正在尝试通过我的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开发的初学者。我也设置了写_ external_storage permission

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

2 个答案:

答案 0 :(得分:3)

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

答案 1 :(得分:0)

嗨,如果你试着参加你的活动,请删除代码中的上下文。

替换

  

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