RingtonePreference无法在Android 8.1上添加新铃声

时间:2019-01-17 08:28:40

标签: android android-preferences ringtonemanager

在xml文件中,我有以下代码。我可以通过在ringtonepref屏幕中单击广告铃声按钮来选择mp3声音,但是当我这样做时,我看到以下异常。这曾经可以工作,但是在更新到android 8之后却无法正常工作。

怎么能

 <RingtonePreference


android:defaultValue="content://settings/system/notification_sound"
        android:key="ringtone_pref"
        android:ringtoneType="all"
        android:title="@string/hr_beep_tone_title"
        android:summary="@string/hr_beep_tone_summary"/> 


 01-17 00:21:15.785 15503-16432/? E/RingtonePickerActivity: Unable to add new ringtone
        java.lang.IllegalArgumentException: Unsupported ringtone type: 7
            at android.media.RingtoneManager.getExternalDirectoryForType(RingtoneManager.java:1088)
            at android.media.RingtoneManager.addCustomExternalRingtone(RingtoneManager.java:1056)
            at com.android.providers.media.RingtonePickerActivity$2.doInBackground(RingtonePickerActivity.java:281)
            at com.android.providers.media.RingtonePickerActivity$2.doInBackground(RingtonePickerActivity.java:278)
            at android.os.AsyncTask$2.call(AsyncTask.java:333)
            at java.util.concurrent.FutureTask.run(FutureTask.java:266)
            at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:245)
            at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1162)
            at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:636)
            at java.lang.Thread.run(Thread.java:764)

2 个答案:

答案 0 :(得分:0)

根据android文档,类型7是TYPE_ALL,而不是TYPE_RINGTONE。我认为您从“铃声”中选择了其他目录中的歌曲。我没有尝试,但是当选择了自定义铃声时,几乎所有的铃声选择器应用程序都将歌曲文件移动到该文件夹​​中,也许您可​​以尝试。

答案 1 :(得分:0)

挖掘android-27源代码,似乎addCustomExternalRingtone正在复制您选择的声音文件,但是给定的参数TYPE_ALL不允许确定要保存的目录。

addCustomExternalRingtone

   @WorkerThread
    public Uri addCustomExternalRingtone(@NonNull final Uri fileUri, final int type)
            throws FileNotFoundException, IllegalArgumentException, IOException {

        ...

        // Choose a directory to save the ringtone. Only one type of installation at a time is
        // allowed. Throws IllegalArgumentException if anything else is given.
        final String subdirectory = getExternalDirectoryForType(type);

        // Find a filename. Throws FileNotFoundException if none can be found.
        final File outFile = Utils.getUniqueExternalFile(mContext, subdirectory,
                Utils.getFileDisplayNameFromUri(mContext, fileUri), mimeType);

        // Copy contents to external ringtone storage. Throws IOException if the copy fails.
        try (final InputStream input = mContext.getContentResolver().openInputStream(fileUri);
                final OutputStream output = new FileOutputStream(outFile)) {
            Streams.copy(input, output);
        }

        // Tell MediaScanner about the new file. Wait for it to assign a {@link Uri}.
        try (NewRingtoneScanner scanner =  new NewRingtoneScanner(outFile)) {
            return scanner.take();
        } catch (InterruptedException e) {
            throw new IOException("Audio file failed to scan as a ringtone", e);
        }
    }

还有getExternalDirectoryForType,实际上会发生错误。

    private static final String getExternalDirectoryForType(final int type) {
        switch (type) {
            case TYPE_RINGTONE:
                return Environment.DIRECTORY_RINGTONES;
            case TYPE_NOTIFICATION:
                return Environment.DIRECTORY_NOTIFICATIONS;
            case TYPE_ALARM:
                return Environment.DIRECTORY_ALARMS;
            default:
                throw new IllegalArgumentException("Unsupported ringtone type: " + type);
        }
    }

问题在于RingtonePickerActivity无法决定选择哪种类型进行保存,并最终给出TYPE_ALL。

似乎您应该覆盖选择文件的重点,然后将uri传递给RingtoneManager.addCustomExternalRingtone或自己保存文件。