我使用下面提供的代码列出我的设备中Arraylist变量中的所有歌曲" al"。然后从该列表中我选择我想要的歌曲的路径然后我正在使用该特定的歌曲路径把它设置为手机铃声。也是我用来设置手机铃声的代码,我在下面给出了。但问题是,当我再次尝试使用相同代码列出所有歌曲时分配手机铃声然后歌曲我之前设置为手机铃声的我没有列在我的ArrayList变量" al"。
//列出所有歌曲路径的代码
ArrayList<String> al;
ArrayList<String> alname;
MediaPlayer mp;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_audio_select);
al=new ArrayList<String>();
alname=new ArrayList<String>();
getAllSongsFromSDCARD();
}
public void getAllSongsFromSDCARD()
{
String[] STAR = { "*" };
Uri allsongsuri = MediaStore.Audio.Media.EXTERNAL_CONTENT_URI;
String selection = MediaStore.Audio.Media.IS_MUSIC + " != 0";
cursor = getContentResolver().query(allsongsuri, STAR, selection, null, null);
if (cursor != null) {
if (cursor.moveToFirst()) {
do {
String song_name = cursor
.getString(cursor
.getColumnIndex(MediaStore.Audio.Media.DISPLAY_NAME));
int song_id = cursor.getInt(cursor
.getColumnIndex(MediaStore.Audio.Media._ID));
String fullpath = cursor.getString(cursor
.getColumnIndex(MediaStore.Audio.Media.DATA));
al.add(fullpath);
String album_name = cursor.getString(cursor
.getColumnIndex(MediaStore.Audio.Media.ALBUM));
alname.add(song_name);
int album_id = cursor.getInt(cursor
.getColumnIndex(MediaStore.Audio.Media.ALBUM_ID));
String artist_name = cursor.getString(cursor
.getColumnIndex(MediaStore.Audio.Media.ARTIST));
int artist_id = cursor.getInt(cursor
.getColumnIndex(MediaStore.Audio.Media.ARTIST_ID));
} while (cursor.moveToNext());
}
cursor.close();
}
}
//设置手机铃声的代码
File newSoundFile = new File(al.get(0));
ContentValues values = new ContentValues();
values.put(MediaStore.MediaColumns.DATA, newSoundFile.getAbsolutePath());
values.put(MediaStore.MediaColumns.TITLE, "aa");
values.put(MediaStore.MediaColumns.MIME_TYPE, "audio/*");
values.put(MediaStore.MediaColumns.SIZE, newSoundFile.length());
values.put(MediaStore.Audio.Media.ARTIST, R.string.app_name);
values.put(MediaStore.Audio.Media.DURATION, 230);
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);
Uri uri = MediaStore.Audio.Media.getContentUriForPath(newSoundFile.getAbsolutePath());
AssignToAllActivity.this.getContentResolver().delete(uri, MediaStore.MediaColumns.DATA + "=\"" + newSoundFile.getAbsolutePath() + "\"", null);
Uri newUri = AssignToAllActivity.this.getContentResolver().insert(uri, values);
try {
getContentResolver().delete(uri, MediaStore.MediaColumns.DATA
+ "=\"" + newSoundFile.getAbsolutePath() + "\"", null);
RingtoneManager manager = new RingtoneManager(AssignToAllActivity.this);
manager.setStopPreviousRingtone(true);
RingtoneManager.setActualDefaultRingtoneUri(AssignToAllActivity.this, RingtoneManager.TYPE_RINGTONE, newUri);
Toast.makeText(AssignToAllActivity.this,"setAsRingTuneSuccess", Toast.LENGTH_LONG).show();
}
catch (Exception e) {
Toast.makeText(AssignToAllActivity.this,"Error", Toast.LENGTH_LONG).show();
}
答案 0 :(得分:0)
您的代码在第二次运行时找不到该歌曲,因为您的代码会将其删除。
getContentResolver().delete(uri, MediaStore.MediaColumns.DATA
+ "=\"" + newSoundFile.getAbsolutePath() + "\"", null);