How to set ringtone in Android from my activity?

后端 未结 9 1481
一个人的身影
一个人的身影 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

    Answer By Vidar is too long and it adds duplicate entries every time you want to set a song as ringtone . Instead you should try this

    Uri newUri=Uri.parse("content://media/external/audio/media/"+ID);  
    try {
          RingtoneManager.setActualDefaultRingtoneUri(context, RingtoneManager.TYPE_RINGTONE, newUri);
        }
    catch (Throwable t) {
    
    
                      }
    
    0 讨论(0)
  • 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!

    0 讨论(0)
  • 2020-11-22 12:52

    I have try these code its help

      private void setRingtone(Context context, String path) {
        if (path == null) {
            return;
        }
        File file = new File(path);
        ContentValues contentValues = new ContentValues();
        contentValues.put(MediaStore.MediaColumns.DATA, file.getAbsolutePath());
        String filterName = path.substring(path.lastIndexOf("/") + 1);
        contentValues.put(MediaStore.MediaColumns.TITLE, filterName);
        contentValues.put(MediaStore.MediaColumns.MIME_TYPE, "audio/mp3");
        contentValues.put(MediaStore.MediaColumns.SIZE, file.length());
        contentValues.put(MediaStore.Audio.Media.IS_RINGTONE, true);
        Uri uri = MediaStore.Audio.Media.getContentUriForPath(path);
        Cursor cursor = context.getContentResolver().query(uri, null, MediaStore.MediaColumns.DATA + "=?", new String[]{path}, null);
        if (cursor != null && cursor.moveToFirst() && cursor.getCount() > 0) {
            String id = cursor.getString(0);
            contentValues.put(MediaStore.Audio.Media.IS_RINGTONE, true);
            context.getContentResolver().update(uri, contentValues, MediaStore.MediaColumns.DATA + "=?", new String[]{path});
            Uri newuri = ContentUris.withAppendedId(uri, Long.valueOf(id));
            try {
                RingtoneManager.setActualDefaultRingtoneUri(context, RingtoneManager.TYPE_RINGTONE, newuri);
                Toast.makeText(context, "Set as Ringtone Successfully.", Toast.LENGTH_SHORT).show();
            } catch (Throwable t) {
                t.printStackTrace();
            }
            cursor.close();
        }
    }
    
    0 讨论(0)
提交回复
热议问题