在Android中以编程方式将声音文件设置为铃声和通知

在Android中以编程方式将声音文件设置为铃声和通知,android,Android,我的应用程序中有两个按钮。一个用于将声音文件设置为铃声,另一个设置为通知。我可以将声音文件设置为铃声,但当我尝试将其设置为通知时,它会删除我先前添加的铃声。声音文件仅显示为通知。我想通过单独的按钮将声音文件设置为铃声和通知 铃声代码: File k = new File(baseDir,fileName); ContentValues values = new ContentValues(); values.put(MediaStore.MediaColumns.DATA, k.getAbso

我的应用程序中有两个按钮。一个用于将声音文件设置为铃声,另一个设置为通知。我可以将声音文件设置为铃声,但当我尝试将其设置为通知时,它会删除我先前添加的铃声。声音文件仅显示为通知。我想通过单独的按钮将声音文件设置为铃声和通知

铃声代码:

File k = new File(baseDir,fileName);
ContentValues values = new ContentValues();

values.put(MediaStore.MediaColumns.DATA, k.getAbsolutePath());
values.put(MediaStore.MediaColumns.TITLE, "Voicetransform ");  

values.put(MediaStore.MediaColumns.MIME_TYPE, "audio/wav");
values.put(MediaStore.MediaColumns.SIZE, k.length());
values.put(MediaStore.Audio.Media.ARTIST, R.string.app_name);
values.put(MediaStore.Audio.Media.IS_RINGTONE, true);
values.put(MediaStore.Audio.Media.IS_NOTIFICATION, false);
values.put(MediaStore.Audio.Media.IS_ALARM, true);
values.put(MediaStore.Audio.Media.IS_MUSIC, false);

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

try {
    RingtoneManager.setActualDefaultRingtoneUri(
            context, RingtoneManager.TYPE_RINGTONE,
            newUri);
} catch (Throwable t) {

}
通知代码:

ContentValues values = new ContentValues();
values.put(MediaStore.MediaColumns.DATA, k.getAbsolutePath());
values.put(MediaStore.MediaColumns.TITLE, "Voicetransform1");
values.put(MediaStore.MediaColumns.MIME_TYPE, "audio/wav");
values.put(MediaStore.MediaColumns.SIZE, k.length());
values.put(MediaStore.Audio.Media.ARTIST, R.string.app_name);
values.put(MediaStore.Audio.Media.IS_RINGTONE, false);
values.put(MediaStore.Audio.Media.IS_NOTIFICATION, true);
values.put(MediaStore.Audio.Media.IS_ALARM, true);
values.put(MediaStore.Audio.Media.IS_MUSIC, false);

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

try {
    RingtoneManager.setActualDefaultRingtoneUri(
        context, RingtoneManager.TYPE_NOTIFICATION,newUri);
} catch (Throwable t) {

}

File k = new File(baseDir,fileName);

试试这个片段,然后在铃声设置中变红

    public boolean setAsRingtone(String filePath) {
    // this function is use to On or Off Default Ringtone
    String[] files = { filePath };
        MediaScannerConnection.scanFile(context, files, null,
                new MediaScannerConnection.OnScanCompletedListener() {
                    @Override
                    public void onScanCompleted(String path, Uri uri) {
                        System.out.println("Ringtone file " + path + " was scanned seccessfully: " + uri);
                        if(path!=null&&!path.isEmpty()){
                            try {
                                RingtoneManager.setActualDefaultRingtoneUri(context, RingtoneManager.TYPE_RINGTONE, uri);
                            } catch (Throwable t) {

                            }
                        }else {
                            String defaultPath = Settings.System.DEFAULT_RINGTONE_URI.getPath();
                            File newSoundFile = new File(defaultPath);
                            Uri newUri = MediaStore.Audio.Media.getContentUriForPath(newSoundFile.getAbsolutePath());
                            try {
                                RingtoneManager.setActualDefaultRingtoneUri(context, RingtoneManager.TYPE_RINGTONE, newUri);
                            } catch (Throwable t) {}
                        }
                    }
                }
        );

        Toast.makeText(context, "Ringtone has been set successfully", Toast.LENGTH_SHORT).show();

      return true;
}

试试这个片段,然后在铃声设置中变红

    public boolean setAsRingtone(String filePath) {
    // this function is use to On or Off Default Ringtone
    String[] files = { filePath };
        MediaScannerConnection.scanFile(context, files, null,
                new MediaScannerConnection.OnScanCompletedListener() {
                    @Override
                    public void onScanCompleted(String path, Uri uri) {
                        System.out.println("Ringtone file " + path + " was scanned seccessfully: " + uri);
                        if(path!=null&&!path.isEmpty()){
                            try {
                                RingtoneManager.setActualDefaultRingtoneUri(context, RingtoneManager.TYPE_RINGTONE, uri);
                            } catch (Throwable t) {

                            }
                        }else {
                            String defaultPath = Settings.System.DEFAULT_RINGTONE_URI.getPath();
                            File newSoundFile = new File(defaultPath);
                            Uri newUri = MediaStore.Audio.Media.getContentUriForPath(newSoundFile.getAbsolutePath());
                            try {
                                RingtoneManager.setActualDefaultRingtoneUri(context, RingtoneManager.TYPE_RINGTONE, newUri);
                            } catch (Throwable t) {}
                        }
                    }
                }
        );

        Toast.makeText(context, "Ringtone has been set successfully", Toast.LENGTH_SHORT).show();

      return true;
}
试试这个

public void setNoti() {
        File file = new File(Environment.getExternalStorageDirectory(),
                "/Your Directory");
        if (!file.exists()) {
            file.mkdirs();
        }

        String path = Environment.getExternalStorageDirectory()
                .getAbsolutePath() + "/Your Directory";

        File f = new File(path + "/", filename + ".mp3");

        Uri mUri = Uri.parse("android.resource://" + getContext().getPackageName() + "/raw/" + filename);
        ContentResolver mCr = getContext().getContentResolver();
        AssetFileDescriptor soundFile;
        try {
            soundFile = mCr.openAssetFileDescriptor(mUri, "r");
        } catch (FileNotFoundException e) {
            soundFile = null;
        }

        try {
            byte[] readData = new byte[1024];
            FileInputStream fis = soundFile.createInputStream();
            FileOutputStream fos = new FileOutputStream(f);
            int i = fis.read(readData);

            while (i != -1) {
                fos.write(readData, 0, i);
                i = fis.read(readData);
            }

            fos.close();
        } catch (IOException io) {
        }

        ContentValues values = new ContentValues();
        values.put(MediaStore.MediaColumns.DATA, f.getAbsolutePath());
        values.put(MediaStore.MediaColumns.TITLE, name1);
        values.put(MediaStore.MediaColumns.MIME_TYPE, "audio/mp3");
        values.put(MediaStore.MediaColumns.SIZE, f.length());
        values.put(MediaStore.Audio.Media.ARTIST, R.string.app_name);
        values.put(MediaStore.Audio.Media.IS_NOTIFICATION, true);


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

        try {
            RingtoneManager.setActualDefaultRingtoneUri(getContext(),
                    RingtoneManager.TYPE_NOTIFICATION, newUri);
            Settings.System.putString(mCr, Settings.System.NOTIFICATION_SOUND,
                    newUri.toString());
            Toast.makeText(getContext(), "Done", Toast.LENGTH_SHORT).show();

        } catch (Throwable t) {

        }
    }
试试这个

public void setNoti() {
        File file = new File(Environment.getExternalStorageDirectory(),
                "/Your Directory");
        if (!file.exists()) {
            file.mkdirs();
        }

        String path = Environment.getExternalStorageDirectory()
                .getAbsolutePath() + "/Your Directory";

        File f = new File(path + "/", filename + ".mp3");

        Uri mUri = Uri.parse("android.resource://" + getContext().getPackageName() + "/raw/" + filename);
        ContentResolver mCr = getContext().getContentResolver();
        AssetFileDescriptor soundFile;
        try {
            soundFile = mCr.openAssetFileDescriptor(mUri, "r");
        } catch (FileNotFoundException e) {
            soundFile = null;
        }

        try {
            byte[] readData = new byte[1024];
            FileInputStream fis = soundFile.createInputStream();
            FileOutputStream fos = new FileOutputStream(f);
            int i = fis.read(readData);

            while (i != -1) {
                fos.write(readData, 0, i);
                i = fis.read(readData);
            }

            fos.close();
        } catch (IOException io) {
        }

        ContentValues values = new ContentValues();
        values.put(MediaStore.MediaColumns.DATA, f.getAbsolutePath());
        values.put(MediaStore.MediaColumns.TITLE, name1);
        values.put(MediaStore.MediaColumns.MIME_TYPE, "audio/mp3");
        values.put(MediaStore.MediaColumns.SIZE, f.length());
        values.put(MediaStore.Audio.Media.ARTIST, R.string.app_name);
        values.put(MediaStore.Audio.Media.IS_NOTIFICATION, true);


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

        try {
            RingtoneManager.setActualDefaultRingtoneUri(getContext(),
                    RingtoneManager.TYPE_NOTIFICATION, newUri);
            Settings.System.putString(mCr, Settings.System.NOTIFICATION_SOUND,
                    newUri.toString());
            Toast.makeText(getContext(), "Done", Toast.LENGTH_SHORT).show();

        } catch (Throwable t) {

        }
    }