我在Android中有一个音板应用程序,用户可以长按按钮将声音保存为铃声,通知或闹钟。一切正常,声音保存到SD卡并设置为默认声音,但声音名称设置为应用程序中的数字位置。例如,如果我保存了标题为“笑”的第一个声音,它将保存到SD卡并在铃声列表中显示为“1”,而不是“笑”。我希望用他们的标题保存声音而不是他们的号码。
我需要在代码中更改以修复此问题?我正在使用数组将声音标题设置为按钮等。这是我保存声音的代码:
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;
}
}
答案 0 :(得分:1)
我知道这是旧的,但它可能对某人有所帮助。删除此行,然后它将从您的ContentValues中选取适当的标题:
sendBroadcast(new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE,
Uri.parse("file://" + path + filename)));
我认为这是重新扫描您刚创建的文件并覆盖媒体商店中的条目,在此过程中删除原始的ContentValues。
答案 1 :(得分:0)
更改
values.put(MediaStore.MediaColumns.TITLE, filename);
到
values.put(MediaStore.MediaColumns.TITLE, "Laughing");
因为 TITLE 会在列表中显示给用户,并且文件名似乎等于“1”。 希望这会有所帮助。