Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why RingtoneManager.setActualDefaultRingtoneUri could not work? [android API-8]

This question could be duplicate of this question but I see some difference in code, so I put a new question.

Problem is that I cannot set picked ringtone in RingtonePicker. I use Support Library and AndroidAnnotations, maybe it can cause such problems (though I doubt it).

I have Fragment with a button in it. When clicking on button a RingtonePicker is fired. User select ringtone and application saves it in SharedPreference. Next time when user open RingtonePicker, previously selected ringtone should be checked, and I can't do that.

Here is my Fragment

@EFragment(R.layout.pref_page)
public class PrefPage extends Fragment {

    @Pref
    MyPrefs_ myPrefs;

    @Click(R.id.ringtone_button)
    public void onClick() {
        Intent intent = new Intent(RingtoneManager.ACTION_RINGTONE_PICKER);
        intent.putExtra(RingtoneManager.EXTRA_RINGTONE_TYPE,
                RingtoneManager.TYPE_NOTIFICATION);

        String uri = myPrefs.beepUri().get();    

        if (uri != "") {      
                    Log.i("Log", "uri is " + uri);              
                    RingtoneManager.setActualDefaultRingtoneUri(
                            getActivity(),
                            RingtoneManager.TYPE_NOTIFICATION,
                            Uri.parse(uri));
        }

        startActivityForResult(intent, 1);
    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (resultCode == -1) {
            Uri uri = data.getParcelableExtra(RingtoneManager.EXTRA_RINGTONE_PICKED_URI);
            if (uri != null) {
                String uriString = uri.toString();
                Log.i("Log", "uriString is " + uriString);
                myPrefs.edit().beepUri().put(uriString).apply();
            }
        }
    }
}

In two words question is - why RingtoneManager.setActualDefaultRingtoneUri is not working here?

UPDATED:

I have WRITE_SETTINGS permission in my manifest.

<uses-permission android:name="android.permission.WRITE_SETTINGS" ></uses-permission>
like image 500
Vitalii Korsakov Avatar asked Nov 12 '12 22:11

Vitalii Korsakov


2 Answers

Ok so I had the same issue as you. I am assuming that the Uri you are passing in is a file in your assets or from the disk that you are storing in a preference file. Working under this assumption, the issue is that you can't shove a Uri into the RingtoneManager and expect it to take it. The file uri should be coming from the content resolver.

I would love it if someone could tell me why that is exactly, but I'm not an expert so I'm accepting it as is for now. That being said, this code will let you take a Uri and set it as the default ringtone.

    //We get the Uri here fro ma file's absolute path.
    Uri ringtoneUri = Uri.parse(file.getAbsolutePath());

    //We now create a new content values object to store all the information 
    //about the ringtone.
    ContentValues values = new ContentValues();
    values.put(MediaStore.MediaColumns.DATA, chosenFile.getAbsolutePath());
    values.put(MediaStore.MediaColumns.TITLE, chosenFile.getName());
    values.put(MediaStore.MediaColumns.SIZE, chosenFile.length());
    values.put(MediaStore.MediaColumns.MIME_TYPE, "audio/mp3");
    values.put(AudioColumns.ARTIST, context.getString(R.string.app_name));
    values.put(AudioColumns.IS_RINGTONE, true);
    values.put(AudioColumns.IS_NOTIFICATION, false);
    values.put(AudioColumns.IS_ALARM, false);
    values.put(AudioColumns.IS_MUSIC, false);

    //Work with the content resolver now
    //First get the file we may have added previously and delete it, 
    //otherwise we will fill up the ringtone manager with a bunch of copies over time.
    Uri uri = MediaStore.Audio.Media.getContentUriForPath(chosenFile.getAbsolutePath());
    context.getContentResolver().delete(uri, MediaStore.MediaColumns.DATA + "=\"" + chosenFile.getAbsolutePath() + "\"", null);

    //Ok now insert it
    Uri newUri = context.getContentResolver().insert(uri, values);

    //Ok now set the ringtone from the content manager's uri, NOT the file's uri
    RingtoneManager.setActualDefaultRingtoneUri(
      context,
      RingtoneManager.TYPE_RINGTONE,
      newUri
    );
like image 131
Andrew T. Avatar answered Nov 12 '22 23:11

Andrew T.


You need to specify the permission to write in to settings in the manifest file.

<uses-permission android:name="android.permission.WRITE_SETTINGS" ></uses-permission>

Also post your logcat output.

like image 2
Durairaj Packirisamy Avatar answered Nov 13 '22 00:11

Durairaj Packirisamy