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

发布于 2024-10-31 09:29:48 字数 1661 浏览 0 评论 0原文

我正在尝试通过我的 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 权限

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

I am trying set a ringtone through my android application. I tried with this code

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


}
}

But I get the error: context cannot be resolved to a type. What does this mean? I am a beginner with android development. I set write_external_storage permission also.

What more do I have to do to run this application successfully?
Thanks in advance

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

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

发布评论

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

评论(2

春花秋月 2024-11-07 09:29:48
    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);
    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);
别闹i 2024-11-07 09:29:48

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

更换线路

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

hi Sarath if r try in your activity the remove the Context in your code.

replace the line

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

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