【发布时间】:2015-12-31 18:44:50
【问题描述】:
我正在构建一个用于播放有声读物的安卓应用。有声读物元数据(标题、作者等)使用以下结构存储在 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 数据库中选择我需要的值,跳过例如有声书的曲目?
谢谢!
【问题讨论】:
标签: java android firebase out-of-memory nosql