How to set ringtone in Android from my activity?

后端 未结 9 1621
一个人的身影
一个人的身影 2020-11-22 12:11

I\'m trying to find a way to set a new default ringtone by code from my Android activity.

I have already downloaded the ringtone into a bytearray.

9条回答
  •  时光说笑
    2020-11-22 12:49

    This is the code i used! i hope it helps..
    This is also the link.

     String exStoragePath =    Environment.getExternalStorageDirectory().getAbsolutePath();
    String path=(exStoragePath +"/media/alarms/"); 
    
    saveas(RingtoneManager.TYPE_RINGTONE); 
    
    sendBroadcast(new Intent(Intent.ACTION_MEDIA_MOUNTED,       Uri.parse("file://"+path+filename+".mp3"
      + Environment.getExternalStorageDirectory()))); 
    
    
     File k = new File(path, filename);
    
    ContentValues values = new ContentValues(4);   
    long current = System.currentTimeMillis();
    values.put(MediaStore.MediaColumns.DATA, path + filename  );
    values.put(MediaStore.MediaColumns.TITLE,  filename ); 
    values.put(MediaStore.Audio.Media.DATE_ADDED, (int) (current / 1000));
    values.put(MediaStore.Audio.Media.MIME_TYPE, "audio/3gpp");
    
    //new
     values.put(MediaStore.Audio.Media.ARTIST, "cssounds ");
    values.put(MediaStore.Audio.Media.IS_RINGTONE, true);
    values.put(MediaStore.Audio.Media.IS_NOTIFICATION, false);
    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);
    

    HAPPY CODING!

提交回复
热议问题