Unable to set as Ringtone because file already exists

喜夏-厌秋 提交于 2019-12-25 07:59:22

问题


I am trying to make app with set as ringtone feature but I got a problem. When I Set as ringtone once it's working. But when I try to set as ringtone for second time, nothing happens. Now the problem is because file already exist. How can I set my code to like...If file already exist proceed to next step Here is my code:

private File rsound;
private final File rpath = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_RINGTONES);

private void s1ring() {
                Boolean success = false;
                rsound = new File(rpath, " Thesound.mp3");
                if (!rsound.exists()) {

                    try {
                        InputStream in = getResources().openRawResource(R.raw.s1a64);
                        FileOutputStream out = new 

FileOutputStream(rsound.getPath());
                        byte[] buff = new byte[1024];
                        int read = 0;

                        try {
                            while ((read = in.read(buff)) > 0) {
                                out.write(buff, 0, read);
                            }
                        } finally {
                            in.close();

                            out.close();
                        }
                    } catch (Exception e) {
                        success = false;

                    }
                } else {
                    success = true;

                }

                if (!success) { 
                   setRingtone();


                }
            }

            private void setRingtone() {
                ContentValues values = new ContentValues();
                   values.put(MediaStore.MediaColumns.DATA, rsound.getAbsolutePath());
                   values.put(MediaStore.MediaColumns.TITLE, "Thesound");
                   values.put(MediaStore.MediaColumns.MIME_TYPE, "audio/*");
                   values.put(MediaStore.Audio.Media.ARTIST, "The ringtones");
                   values.put(MediaStore.Audio.Media.IS_RINGTONE, true);
                   values.put(MediaStore.Audio.Media.IS_NOTIFICATION, true);
                   values.put(MediaStore.Audio.Media.IS_ALARM, true);
                   values.put(MediaStore.Audio.Media.IS_MUSIC, true);

                   Uri uri = MediaStore.Audio.Media.getContentUriForPath(rsound.getAbsolutePath());
                   getContentResolver().delete(uri, MediaStore.MediaColumns.DATA + "=\"" + rsound.getAbsolutePath() + "\"",
                           null);
                   Uri newUri = getContentResolver().insert(uri, values);

                   RingtoneManager.setActualDefaultRingtoneUri(
                           S1.this, RingtoneManager.TYPE_RINGTONE,
                           newUri);
                   Toast.makeText(getApplicationContext(), "Ringtone set successfully",
                           Toast.LENGTH_SHORT).show();

            }


            }
        );

回答1:


adjust your code like this:

            if (!rsound.exists()) {
                //your create sound file code here.

            } else {
                //call set ringtone method also for the case file exists:
                setRingtone();
            }



回答2:


Audio is set as ringtone only once, but solution to this problem is - If you are trying to run the same code again, you'll be inserting a duplicate entry into MediaStore's table, but the SQLite database won't allow you. You have to either rename your file and add another instance of it, or go in, remove the entry, and then try again. So I removed that entry every time and then insert it again.

Uri uri = MediaStore.Audio.Media.getContentUriForPath(k.getAbsolutePath());
getContentResolver().delete(uri, MediaStore.MediaColumns.DATA + "=\"" + k.getAbsolutePath() + "\"", null);
Uri newUri = getContentResolver().insert(uri, values);

RingtoneManager.setActualDefaultRingtoneUri(activity.this,
        RingtoneManager.TYPE_RINGTONE, newUri);


来源:https://stackoverflow.com/questions/22268504/unable-to-set-as-ringtone-because-file-already-exists

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!