java - 使用声音的标题而不是数字位置将声音保存为铃声

标签 java android

我在 Android 中有一个音板应用程序,用户可以长按按钮将声音保存为铃声、通知或闹钟。一切正常,声音被保存到 sdcard 并设置为默认声音,但声音名称被设置为它们在应用程序中的数字位置。例如,如果我保存了第一个名为“laughing”的声音,它将保存到 sdcard 并在铃声列表中显示为“1”,而不是“laughing”。我希望声音以其标题而不是编号保存。

我需要在我的代码中更改什么来解决这个问题?我正在使用一个数组来为按钮等设置声音标题。这是我保存声音的代码:

public boolean setAsTone(int ressound, String filename, String toneType) {

    Log.v("File Name", "" + filename);
    Log.v("Tone Type", "" + toneType);
    byte[] buffer = null;
    InputStream fIn = getBaseContext().getResources().openRawResource(ressound);
    int size = 0;

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

    }
    if (toneType.contains(getString(R.string.ringtone))) {
        path = "/sdcard/media/audio/"
                + getString(R.string.ringtone).toLowerCase() + "/";
        Log.v("Path", "" + path);
    } else if (toneType.contains(getString(R.string.notification))) {
        path = "/sdcard/media/audio/"
                + getString(R.string.notification).toLowerCase() + "/";
        Log.v("Path", "" + path);
    } else if (toneType.contains(getString(R.string.alarm))) {
        path = "/sdcard/media/audio/"
                + getString(R.string.alarm).toLowerCase() + "/";
        Log.v("Path", "" + path);
    }

    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_SCANNER_SCAN_FILE,
            Uri.parse("file://" + path + filename)));

    File ringtoneFile = new File(path, filename);

    ContentValues values = new ContentValues();
    values.put(MediaStore.MediaColumns.DATA, ringtoneFile.getAbsolutePath());
    values.put(MediaStore.MediaColumns.TITLE, filename);
    values.put(MediaStore.MediaColumns.MIME_TYPE, "audio/ogg");
    values.put(MediaStore.Audio.Media.ARTIST, "Laughing");
    Log.v("getAbsolute Path", "" + ringtoneFile.getAbsolutePath());
    if (toneType.contains(getString(R.string.ringtone))) {
        values.put(MediaStore.Audio.Media.IS_RINGTONE, true);
        values.put(MediaStore.Audio.Media.IS_NOTIFICATION, false);
        values.put(MediaStore.Audio.Media.IS_ALARM, false);
        type = RingtoneManager.TYPE_RINGTONE;
        Log.v("Ring Type", "" + type);
    } else if (toneType.contains(getString(R.string.notification))) {
        values.put(MediaStore.Audio.Media.IS_RINGTONE, false);
        values.put(MediaStore.Audio.Media.IS_NOTIFICATION, true);
        values.put(MediaStore.Audio.Media.IS_ALARM, false);
        type = RingtoneManager.TYPE_NOTIFICATION;
        Log.v("Ring Type", "" + type);
    } else if (toneType.contains(getString(R.string.alarm))) {
        values.put(MediaStore.Audio.Media.IS_RINGTONE, false);
        values.put(MediaStore.Audio.Media.IS_NOTIFICATION, false);
        values.put(MediaStore.Audio.Media.IS_ALARM, true);
        type = RingtoneManager.TYPE_ALARM;
        Log.v("Ring Type", "" + type);
    }

    values.put(MediaStore.Audio.Media.IS_MUSIC, true);

    // Insert it into the database
    Uri ringtoneUri = null;
    Uri uri = MediaStore.Audio.Media.getContentUriForPath(ringtoneFile
            .getAbsolutePath());
    Log.v("Uri Path",""+uri);
    getContentResolver().delete(
            uri,
            MediaStore.MediaColumns.DATA + "=\""
                    + ringtoneFile.getAbsolutePath() + "\"", null);

    ringtoneUri = this.getContentResolver().insert(uri, values);
    Log.v("Ringtone Uri", "" + ringtoneUri);
    Log.v("Final Ring Type", "" + type);
    RingtoneManager.setActualDefaultRingtoneUri(getApplicationContext(),
            type, ringtoneUri);
    RingtoneManager.getActualDefaultRingtoneUri(getApplicationContext(), type);
    return true;
}

最佳答案

我知道这是旧的,但它可能对某些人有帮助。删除此行,然后它将从您的 ContentValues 中获取正确的标题:

sendBroadcast(new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE,
        Uri.parse("file://" + path + filename)));

我相信这是在重新扫描您刚刚创建的文件并覆盖媒体存储中的条目,并在此过程中删除您的原始 ContentValues。

关于java - 使用声音的标题而不是数字位置将声音保存为铃声,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8172539/

相关文章:

java - JPA @Formula 没有架构名称或使用实体类进行配置

java - 未在 prepare 方法中设置的属性

java - 安卓;在类主体中声明 edittext(在任何方法之外)

android - 在照片/视频模式下启动 HTC 相机

android - 以管理员身份运行 Android 应用

android - 展开的 CollapsingToolbarLayout 中的标题显示不正确

Java/C/C++/C#/PHP 到 Pascal 转换器?

java - IE 无法理解 Content-disposition header 字段,但适用于 Chrome

java - 可以抛出运行时异常?

android - 16 位 UUID 而不是 128 位 UUID 用于 android 中的蓝牙服务的可能性