我正在开发一个播放有声读物的安卓应用。这些有声读物的元数据(如标题、作者等)存储在Firebase数据库中,使用以下结构:
root
--- audioBooks
--- <individual hash for each book>
--- author: "Ray Bradbury"
--- title: "Fahrenheit 451"
--- finished: false
--- key: <individual hash for each book>
--- cover
--- b64Cover: <b64-encoded image>
--- backgroundColor: -14473711
--- ...
--- tracks
--- 0
--- title: "Track 1"
--- duration: 361273
--- finished: false
--- currentPosition: 12345
--- ...
--- 1
--- ...
--- currentTrack
--- title: "Track 1"
--- duration: 361273
--- finished: false
--- currentPosition: 12345
--- ...
我有三个模型类,分别是AudioBook、AudioBookTrack和AudioBookCover(为了更好地概览,已删除setter和getter)。
public class AudioBook {
private String title;
private String author;
private int duration;
private boolean finished;
private String key;
private AudioBookCover cover;
private ArrayList<AudioBookTrack> tracks;
private AudioBookTrack currentTrack;
public AudioBook() {
}
public AudioBook(String title, String author, boolean finished, String key, AudioBookCover cover, ArrayList<AudioBookTrack> tracks, AudioBookTrack currentTrack) {
this.title = title;
this.author = author;
this.finished = finished;
this.key = key;
this.cover = cover;
this.tracks = tracks;
this.currentTrack = currentTrack;
}
}
public class AudioBookTrack {
private String title;
private String album;
private String author;
private String filePath;
private int duration;
private int currentPosition;
private int index;
private boolean finished;
private String key;
public AudioBookTrack() {
}
public AudioBookTrack(String title, String album, String author, String filePath, int duration, int currentPosition, int index, boolean finished, String key) {
this.title = title;
this.album = album;
this.author = author;
this.filePath = filePath;
this.duration = duration;
this.currentPosition = currentPosition;
this.index = index;
this.finished = finished;
this.key = key;
}
}
public class AudioBookCover {
private String b64Cover;
private int backgroundColor;
private int primaryColor;
private int secondaryColor;
private int detailColor;
public AudioBookCover() {
}
public AudioBookCover(String b64Cover, int backgroundColor, int primaryColor, int secondaryColor, int detailColor) {
this.b64Cover = b64Cover;
this.backgroundColor = backgroundColor;
this.primaryColor = primaryColor;
this.secondaryColor = secondaryColor;
this.detailColor = detailColor;
}
}
在Firebase数据库中消耗数据并将其转换为模型类没有问题。但是,我现在想使用FirebaseRecyclerViewAdapter在RecyclerView中列出所有音频书籍。这是代码,在片段的onCreateView()方法中执行:
mAudioBooksList = (RecyclerView) view.findViewById(R.id.audiobooks_list);
mAudioBooksList.setHasFixedSize(true);
ref = new Firebase(Constants.FIREBASE_URL).child("audioBooks");
mAdapter = new FirebaseRecyclerViewAdapter<AudioBook, AudioBooksViewHolder>(AudioBook.class, R.layout.audiobook_grid_item, AudioBooksViewHolder.class, ref) {
@Override
public void populateViewHolder(AudioBooksViewHolder audioBooksViewHolder, AudioBook audioBook) {
audioBooksViewHolder.author.setText(audioBook.getAuthor());
audioBooksViewHolder.title.setText(audioBook.getTitle());
try {
byte[] byteArray = Base64.decode(audioBook.getCover().getB64Cover());
Bitmap coverBitmap = BitmapFactory.decodeByteArray(byteArray, 0, byteArray.length);
audioBooksViewHolder.cover.setImageBitmap(coverBitmap);
}
catch(IOException e) {
Log.e(TAG, "Could not decode album cover: " + e.getMessage());
}
}
};
mAudioBooksList.setAdapter(mAdapter);
这是ViewHolder:
private static class AudioBooksViewHolder extends RecyclerView.ViewHolder {
TextView author;
TextView title;
ImageView cover;
public AudioBooksViewHolder(View itemView) {
super(itemView);
author = (TextView)itemView.findViewById(R.id.audiobook_grid_author);
title = (TextView)itemView.findViewById(R.id.audiobook_grid_title);
cover = (ImageView) itemView.findViewById(R.id.audiobook_grid_cover);
}
现在这段代码基本可以运行(我可以看到屏幕上显示的值),然而很快,应用程序由于内存不足错误而崩溃:
Uncaught exception in Firebase runloop (2.4.0). Please report to support@firebase.com
java.lang.OutOfMemoryError: Failed to allocate a 35116844 byte allocation with 16773184 free bytes and 32MB until OOM
at java.lang.StringFactory.newStringFromChars(Native Method)
at java.lang.AbstractStringBuilder.toString(AbstractStringBuilder.java:629)
at java.lang.StringBuilder.toString(StringBuilder.java:663)
...
我在一台物理的Nexus 5上测试了这个应用程序(没有模拟器)。 我猜想这是因为我的嵌套模型(AudioBooks内包含AudioBookTracks)导致了几百个Java对象。不幸的是,我不知道如何解决这个问题。如果只需要列出音频书籍,我就不需要它们对应的音频书籍轨迹,但由于音频书籍轨迹是音频书籍的子元素,它们也被转换为 Java 对象。 另一方面,我还尝试通过使用 ref.limitToFirst(2) 来限制显示的音频书籍数量。然而,应用程序仍会崩溃,但直到崩溃需要更长的时间。
您是否看到了导致问题的代码问题?或者是否有可能仅从Firebase数据库中选择我需要的值,跳过例如音频书籍轨迹之类的内容?
谢谢!