在android中以编程方式从原始文件夹设置为默认铃声

时间:2016-01-13 10:36:47

标签: android android-file

我尝试了这个代码,它的工作正常,可以将声音文件加载到铃声目录。我可以手动从弹出窗口中选择声音..但是它无法以编程方式设置为默认铃声。帮助我将声音设置为默认铃声以编程方式

        setContentView(R.layout.activity_main);
      String exStoragePath = Environment.getExternalStorageDirectory().getAbsolutePath();
     path=(exStoragePath +"/media/alarms/");
            // saveas();
        saveas(RingtoneManager.TYPE_RINGTONE);

    }

    // In this method, we need to copy the mp3 file to the sd card location from
    // where android picks up ringtone files
    // After copying, we make the mp3 as current ringtone
public boolean saveas(int type) {
enter code here
    byte[] buffer = null;
    InputStream fIn = getBaseContext().getResources().openRawResource(
            R.raw.sound);
    int size = 0;

    try {
        size = fIn.available();
        buffer = new byte[size];
        fIn.read(buffer);
        fIn.close();
    } catch (IOException e) {
        return false;
    }


    String filename = "sound";

    boolean exists = (new File(path)).exists();
    if (!exists) {
        new File(path).mkdirs();
    }

    FileOutputStream save;
    try {
        save = new FileOutputStream(path + filename);
        save.write(buffer);
        save.flush();
        save.close();
    } catch (FileNotFoundException e) {
        return false;
    } catch (IOException e) {

        return false;

    }

    sendBroadcast(new Intent(Intent.ACTION_MEDIA_MOUNTED, Uri.parse("file://" + path + filename + ".mp3"
            + Environment.getExternalStorageDirectory())));
enter code here
    File k = new File(path, filename);

    ContentValues values = new ContentValues();
    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/mp3");

    //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, false);
    values.put(MediaStore.Audio.Media.IS_MUSIC, false);

RingtoneManager.getRingtone(context,this.getContentResolver()
    .insert(MediaStore.Audio.Media.getContentUriForPath(k
            .getAbsolutePath()), values));



    return true;

}

2 个答案:

答案 0 :(得分:2)

删除此代码 RingtoneManager.getRingtone(context,this.getContentResolver() .insert(MediaStore.Audio.Media.getContentUriForPath(k .getAbsolutePath()), values));

并在返回true和其工作

之前放置这些行代码
 Uri newUri = this.getContentResolver()
            .insert(MediaStore.Audio.Media.getContentUriForPath(k
                    .getAbsolutePath()), values);
    RingtoneManager.setActualDefaultRingtoneUri(MainActivity.this, RingtoneManager.TYPE_RINGTONE, newUri);

答案 1 :(得分:0)

 try {
                Uri notification = Uri.parse("android.resource://" + getPackageName() + "/" + R.raw.phone_funny_bell);
                Ringtone r = RingtoneManager.getRingtone(getApplicationContext(), notification);
                r.play();

                new Handler().postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        r.stop();
                    }
                }, 3000);
            } catch (Exception e) {
                e.printStackTrace();
            }

使用这个。这将起作用。

在上述 onMessageReceived

内的 MyFirebaseMessagingService 上放置以上代码