我正在尝试删除我的应用中的音频文件。当我点击删除时,该文件被成功删除,该项目将从列表视图中删除。但是当我打开应用程序时,该歌曲再次出现在列表视图中,即使它现在无法播放。我也试过通过股票音乐应用打开歌曲,但它并没有打开。如果我检查外部存储器的路径位置,则找不到它。我在哪里我错了?困惑...
MenuItem miDelete = menu.add(0, 0, 0, "Delete");
miDelete.setIcon(R.drawable.delete);
miDelete.setShowAsAction(MenuItem.SHOW_AS_ACTION_ALWAYS);
miDelete.setOnMenuItemClickListener(new OnMenuItemClickListener() {
@Override
public boolean onMenuItemClick(MenuItem item) {
Iterator<String> it = songAdt.getCheckedItems()
.values().iterator();
Log.d("sparse boolean", ""
+ songAdt.getCheckedItems().size());
for (int i = 0; i < songAdt.getCheckedItems().size(); i++) {
if (it.hasNext()) {
int posItem = Integer.parseInt(it.next());
Song song = (Song) songView
.getItemAtPosition(posItem);
Log.d("file del path", song.getPathId());
File file = new File(song.getPathId());
files.add(file);
positionItem.add(posItem);
}
}
createDialog(files, positionItem);
return false;
}
});
public void createDialog(final List<File> files2,
final List<Integer> position) {
AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(
getSherlockActivity());
// set title
alertDialogBuilder.setTitle("Delete");
// set dialog message
alertDialogBuilder
.setMessage("Confirm delete the selected track")
.setCancelable(false)
.setPositiveButton("Yes",
new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int id) {
for (int i = 0; i < files2.size(); i++) {
Log.d("file del path2", "" + files2.get(i));
if (files2.get(i).exists()) {
Log.d("file exists", "true");
if (files2.get(i).canWrite()) {
Log.d("file write", "true");
fileDeleted = files2.get(i)
.delete();
Log.d("file deleted??", ""
+ fileDeleted); // returns true
} else {
Log.d("file locked", "true");
}
} else {
Log.d("file not exist", "true");
}
if (fileDeleted) {
countDeleted++;
songList.remove((int)position.get(i));
getSherlockActivity().setTitle(
R.string.app_name);
songAdt.notifyDataSetChanged();
}
}
Toast.makeText(getSherlockActivity().getBaseContext(),
countDeleted + " tracks Deleted",
Toast.LENGTH_SHORT).show();
countDeleted = 0;
dialog.dismiss();
}
})
.setNegativeButton("No", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int id) {
Toast.makeText(getSherlockActivity().getBaseContext(),
"Canceled", Toast.LENGTH_SHORT).show();
dialog.dismiss();
}
});
// create alert dialog
AlertDialog alertDialog = alertDialogBuilder.create();
// show it
alertDialog.show();
}
更新 这就是我在onCreate中获取歌曲的方式。
public ArrayList<Song> getSongList(Context context) {
// retrieve song info
ContentResolver musicResolver = context.getContentResolver();
Uri musicUri = android.provider.MediaStore.Audio.Media.EXTERNAL_CONTENT_URI;
// Fetch only music and not alarms,notification tones
String selection = AudioColumns.IS_MUSIC + "!= 0";
Cursor musicCursor = musicResolver.query(musicUri, null, selection,
null, null);
if (musicCursor != null && musicCursor.moveToFirst()) {
// get columns
int titleColumn = musicCursor.getColumnIndex(MediaColumns.TITLE);
int idColumn = musicCursor.getColumnIndex(AudioColumns.ALBUM_ID);
int audioId = musicCursor.getColumnIndex(AudioColumns._ID);
int artistColumn = musicCursor.getColumnIndex(AudioColumns.ARTIST);
int column_index = musicCursor.getColumnIndex(MediaColumns.DATA);
int album_index = musicCursor.getColumnIndex(AudioColumns.ALBUM);
int duration_index = musicCursor
.getColumnIndex(AudioColumns.DURATION);
int size_index = musicCursor.getColumnIndex(MediaColumns.SIZE);
String pathId = null;
// add songs to list
do {
long albumId = musicCursor.getLong(idColumn);
if (column_index != -1) {
pathId = musicCursor.getString(column_index);
}
String thisTitle = musicCursor.getString(titleColumn);
String thisArtist = musicCursor.getString(artistColumn);
String thisAlbum = musicCursor.getString(album_index);
String thisDuration = musicCursor.getString(duration_index);
String thisSize = musicCursor.getString(size_index);
long thisAudioId = musicCursor.getLong(audioId);
songList.add(new Song(albumId, thisTitle, thisArtist, pathId,
thisAlbum, thisDuration, thisSize, thisAudioId));
} while (musicCursor.moveToNext());
return songList;
}
return null;
}