Как установить рингтон программно в oreo 8.0 - PullRequest
0 голосов
/ 25 августа 2018

Я могу установить мелодию звонка программно между 4,0 и 6,0 (Зефир), но не могу установить после 6,0 (Зефир).

Не могли бы вы сообщить мне, как решить эту проблему, чтобы я мог установить мелодию звонка для устройств с Android Oreo 8.0 и 8.1 версии?

  public void setRingtone(String filepath) {
        File ringtoneFile = new File(filepath);
        ContentValues content = new ContentValues();
        content.put(MediaStore.MediaColumns.DATA, ringtoneFile.getAbsolutePath());
        content.put(MediaStore.MediaColumns.TITLE, "Ring");
        content.put(MediaStore.MediaColumns.SIZE, ringtoneFile.length());
        content.put(MediaStore.MediaColumns.MIME_TYPE, "audio/*");
        //  content.put(MediaStore.Audio.Media.ARTIST, "Madonna");
        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, true);

        Uri uri = MediaStore.Audio.Media.getContentUriForPath(ringtoneFile.getAbsolutePath());

        getContentResolver().delete(uri, MediaStore.MediaColumns.DATA + "=\"" + ringtoneFile.getAbsolutePath() + "\"", null);
        Uri newUri = getContentResolver().insert(uri, content);
        System.out.println("uri==" + uri);
        Log.i("TAG", "the ringtone uri is :" + newUri);
        RingtoneManager.setActualDefaultRingtoneUri(getApplicationContext(), RingtoneManager.TYPE_RINGTONE, newUri);

        Toast.makeText(this, "Ringtone set success!", Toast.LENGTH_SHORT).show();
    }

Спасибо.

1 Ответ

0 голосов
/ 25 августа 2018

Добавить разрешение URI для вашего проекта

AndroidManifest.xml

 <provider
        android:name="android.support.v4.content.FileProvider"
        android:authorities="${applicationId}.provider"
        android:exported="false"
        android:grantUriPermissions="true">
        <meta-data
            android:name="android.support.FILE_PROVIDER_PATHS"
            android:resource="@xml/provider_paths" />
    </provider>

создать xml папку в res и создать provider_path.xml в res / XML как это

provider_paths.xml

    <?xml version="1.0" encoding="utf-8"?>
    <paths xmlns:android="http://schemas.android.com/apk/res/android">
    <external-path name="external_files" path="."/>
    </paths>

Надеюсь, это поможет

...