我正在构建一个活动,我希望使用Loader填充音频。
public class MainActivity extends BaseActivity implements
LoaderManager.LoaderCallbacks<Cursor> {
private static final int INTERNAL_CURSOR_ID = 0;
private static final int EXTERNAL_CURSOR_ID = 1;
private ArrayList<Audio> audios = new ArrayList<>();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initLoader();
}
private void initLoader() {
try {
getSupportLoaderManager().initLoader(INTERNAL_CURSOR_ID, null, this);
getSupportLoaderManager().initLoader(EXTERNAL_CURSOR_ID, null, this);
} catch (SecurityException e) {
} catch (IllegalArgumentException e) {
}
}
private static final String[] INTERNAL_COLUMNS = new String[]{
MediaStore.Audio.Media._ID,
MediaStore.Audio.Media.ALBUM,
MediaStore.Audio.Media.ARTIST,
MediaStore.Audio.Media.COMPOSER,
MediaStore.Audio.Media.DATA,
MediaStore.Audio.Media.DATE_ADDED,
MediaStore.Audio.Media.DATE_MODIFIED,
MediaStore.Audio.Media.DISPLAY_NAME,
MediaStore.Audio.Media.DURATION,
MediaStore.Audio.Media.IS_ALARM,
MediaStore.Audio.Media.IS_MUSIC,
MediaStore.Audio.Media.IS_NOTIFICATION,
MediaStore.Audio.Media.IS_PODCAST,
MediaStore.Audio.Media.IS_RINGTONE,
MediaStore.Audio.Media.MIME_TYPE,
MediaStore.Audio.Media.SIZE,
MediaStore.Audio.Media.TITLE
};
private static final String[] EXTERNAL_COLUMNS = new String[]{
MediaStore.Audio.Media._ID,
MediaStore.Audio.Media.ALBUM,
MediaStore.Audio.Media.ARTIST,
MediaStore.Audio.Media.COMPOSER,
MediaStore.Audio.Media.DATA,
MediaStore.Audio.Media.DATE_ADDED,
MediaStore.Audio.Media.DATE_MODIFIED,
MediaStore.Audio.Media.DISPLAY_NAME,
MediaStore.Audio.Media.DURATION,
MediaStore.Audio.Media.IS_ALARM,
MediaStore.Audio.Media.IS_MUSIC,
MediaStore.Audio.Media.IS_NOTIFICATION,
MediaStore.Audio.Media.IS_PODCAST,
MediaStore.Audio.Media.IS_RINGTONE,
MediaStore.Audio.Media.MIME_TYPE,
MediaStore.Audio.Media.SIZE,
MediaStore.Audio.Media.TITLE
};
@Override
public Loader<Cursor> onCreateLoader(int id, Bundle args) {
ArrayList<String> selectionArgsList = new ArrayList<String>();
String selection;
Uri baseUri;
String[] projection;
switch (id) {
case INTERNAL_CURSOR_ID:
baseUri = MediaStore.Audio.Media.INTERNAL_CONTENT_URI;
projection = INTERNAL_COLUMNS;
break;
case EXTERNAL_CURSOR_ID:
baseUri = MediaStore.Audio.Media.EXTERNAL_CONTENT_URI;
projection = EXTERNAL_COLUMNS;
break;
default:
return null;
}
selection = "(_DATA LIKE ?)";
selectionArgsList.add("%");
String[] selectionArgs = selectionArgsList.toArray(new String[selectionArgsList.size()]);
return new CursorLoader(this, baseUri, projection, selection, selectionArgs,
MediaStore.Audio.Media.DEFAULT_SORT_ORDER);
}
@Override
public void onLoadFinished(Loader<Cursor> loader, Cursor data) {
if (data == null) {
return;
}
if (data.moveToFirst()) {
do {
int id = data.getInt(data.getColumnIndexOrThrow(MediaStore.Audio.Media._ID));
String album = data.getString(data.getColumnIndexOrThrow(MediaStore.Audio.Media.ALBUM));
String artist = data.getString(data.getColumnIndexOrThrow(MediaStore.Audio.Media.ARTIST));
String composer =
data.getString(data.getColumnIndexOrThrow(MediaStore.Audio.Media.COMPOSER));
String _data = data.getString(data.getColumnIndexOrThrow(MediaStore.Audio.Media.DATA));
int dateAdded = data.getInt(data.getColumnIndexOrThrow(MediaStore.Audio.Media.DATE_ADDED));
int dateModified =
data.getInt(data.getColumnIndexOrThrow(MediaStore.Audio.Media.DATE_MODIFIED));
String displayName =
data.getString(data.getColumnIndexOrThrow(MediaStore.Audio.Media.DISPLAY_NAME));
int duration = data.getInt(data.getColumnIndexOrThrow(MediaStore.Audio.Media.DURATION));
int isAlarm = data.getInt(data.getColumnIndexOrThrow(MediaStore.Audio.Media.IS_ALARM));
int isMusic = data.getInt(data.getColumnIndexOrThrow(MediaStore.Audio.Media.IS_MUSIC));
int isNotification =
data.getInt(data.getColumnIndexOrThrow(MediaStore.Audio.Media.IS_NOTIFICATION));
int isPodcast = data.getInt(data.getColumnIndexOrThrow(MediaStore.Audio.Media.IS_PODCAST));
int isRingtone =
data.getInt(data.getColumnIndexOrThrow(MediaStore.Audio.Media.IS_RINGTONE));
String mimeType =
data.getString(data.getColumnIndexOrThrow(MediaStore.Audio.Media.MIME_TYPE));
int size = data.getInt(data.getColumnIndexOrThrow(MediaStore.Audio.Media.SIZE));
String title = data.getString(data.getColumnIndexOrThrow(MediaStore.Audio.Media.TITLE));
String folderName = new File(new File(_data).getParent()).getName();
String folderPath = new File(_data).getParent();
Audio audio = new Audio();
audio.setId(id);
audio.setAlbum(album);
audio.setArtist(artist);
audio.setComposer(composer);
audio.setData(_data);
audio.setDateAdded(dateAdded);
audio.setDateModified(dateModified);
audio.setDisplayName(displayName);
audio.setDuration(duration);
audio.setIsAlarm(isAlarm);
audio.setIsMusic(isMusic);
audio.setIsNotification(isNotification);
audio.setIsPodcast(isPodcast);
audio.setIsRingtone(isRingtone);
audio.setMimeType(mimeType);
audio.setSize(size);
audio.setTitle(title);
audios.add(audio);
} while (data.moveToNext());
}
this.runOnUiThread(new Runnable() {
@Override
public void run() {
// update audios to RecyclerView
}
});
}
这是型号:Audio.class:
public class Audio implements Parcelable {
private int id;
private String album;
private String artist;
private String composer;
private String data;
private int dateAdded;
private int dateModified;
private String displayName;
private int duration;
private int isAlarm;
private int isMusic;
private int isNotification;
private int isPodcast;
private int isRingtone;
private String mimeType;
private int size;
private String title;
public Audio() {
}
protected Audio(Parcel in) {
id = in.readInt();
album = in.readString();
artist = in.readString();
composer = in.readString();
data = in.readString();
dateAdded = in.readInt();
dateModified = in.readInt();
displayName = in.readString();
duration = in.readInt();
isAlarm = in.readInt();
isMusic = in.readInt();
isNotification = in.readInt();
isPodcast = in.readInt();
isRingtone = in.readInt();
mimeType = in.readString();
size = in.readInt();
title = in.readString();
}
public static final Creator<Audio> CREATOR = new Creator<Audio>() {
@Override
public Audio createFromParcel(Parcel in) {
return new Audio(in);
}
@Override
public Audio[] newArray(int size) {
return new Audio[size];
}
};
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (!(o instanceof Audio)) return false;
Audio photo = (Audio) o;
return id == photo.id;
}
@Override
public int hashCode() {
return id;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getAlbum() {
return album;
}
public void setAlbum(String album) {
this.album = album;
}
public String getArtist() {
return artist;
}
public void setArtist(String artist) {
this.artist = artist;
}
public String getComposer() {
return composer;
}
public void setComposer(String composer) {
this.composer = composer;
}
public String getData() {
return data;
}
public void setData(String data) {
this.data = data;
}
public int getDateAdded() {
return dateAdded;
}
public void setDateAdded(int dateAdded) {
this.dateAdded = dateAdded;
}
public int getDateModified() {
return dateModified;
}
public void setDateModified(int dateModified) {
this.dateModified = dateModified;
}
public String getDisplayName() {
return displayName;
}
public void setDisplayName(String displayName) {
this.displayName = displayName;
}
public int getDuration() {
return duration;
}
public void setDuration(int duration) {
this.duration = duration;
}
public int getIsAlarm() {
return isAlarm;
}
public void setIsAlarm(int isAlarm) {
this.isAlarm = isAlarm;
}
public int getIsMusic() {
return isMusic;
}
public void setIsMusic(int isMusic) {
this.isMusic = isMusic;
}
public int getIsNotification() {
return isNotification;
}
public void setIsNotification(int isNotification) {
this.isNotification = isNotification;
}
public int getIsPodcast() {
return isPodcast;
}
public void setIsPodcast(int isPodcast) {
this.isPodcast = isPodcast;
}
public int getIsRingtone() {
return isRingtone;
}
public void setIsRingtone(int isRingtone) {
this.isRingtone = isRingtone;
}
public String getMimeType() {
return mimeType;
}
public void setMimeType(String mimeType) {
this.mimeType = mimeType;
}
public int getSize() {
return size;
}
public void setSize(int size) {
this.size = size;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
@Override public int describeContents() {
return 0;
}
@Override public void writeToParcel(Parcel parcel, int i) {
parcel.writeInt(id);
parcel.writeString(album);
parcel.writeString(artist);
parcel.writeString(composer);
parcel.writeString(data);
parcel.writeInt(dateAdded);
parcel.writeInt(dateModified);
parcel.writeString(displayName);
parcel.writeInt(duration);
parcel.writeInt(isAlarm);
parcel.writeInt(isMusic);
parcel.writeInt(isNotification);
parcel.writeInt(isPodcast);
parcel.writeInt(isRingtone);
parcel.writeString(mimeType);
parcel.writeInt(size);
parcel.writeString(title);
}
}
为什么光标会为所有音频返回重复项?或者设备上是否存在导致返回这些重复项的内容。