我正在开发一个显示所有专辑图片封面的应用。但它崩溃了这个错误:
11-10 18:08:48.148 16187-16187/com.xrobot.andrew.musicalbums E/AndroidRuntime﹕ FATAL EXCEPTION: main
Process: com.xrobot.andrew.musicalbums, PID: 16187
java.lang.OutOfMemoryError: Failed to allocate a 1459276 byte allocation with 421736 free bytes and 411KB until OOM
at dalvik.system.VMRuntime.newNonMovableArray(Native Method)
at android.graphics.BitmapFactory.nativeDecodeStream(Native Method)
at android.graphics.BitmapFactory.decodeStreamInternal(BitmapFactory.java:635)
at android.graphics.BitmapFactory.decodeStream(BitmapFactory.java:611)
at android.graphics.BitmapFactory.decodeFile(BitmapFactory.java:391)
at android.graphics.BitmapFactory.decodeFile(BitmapFactory.java:417)
at android.graphics.drawable.Drawable.createFromPath(Drawable.java:1230)
at com.xrobot.andrew.musictest2.SongAdapter.getView(SongAdapter.java:73)
at android.widget.AbsListView.obtainView(AbsListView.java:2346)
at android.widget.ListView.makeAndAddView(ListView.java:1875)
at android.widget.ListView.fillDown(ListView.java:702)
at android.widget.ListView.correctTooLow(ListView.java:1484)
at android.widget.ListView.fillGap(ListView.java:676)
at android.widget.AbsListView.trackMotionScroll(AbsListView.java:5036)
at android.widget.AbsListView$FlingRunnable.run(AbsListView.java:4584)
at android.view.Choreographer$CallbackRecord.run(Choreographer.java:858)
at android.view.Choreographer.doCallbacks(Choreographer.java:670)
at android.view.Choreographer.doFrame(Choreographer.java:603)
at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:844)
at android.os.Handler.handleCallback(Handler.java:746)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5443)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:728)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618)
onCreate方法中的部分代码:
...
while(...){
String thisCover = getCoverArtPath(cursor.getLong(6), getApplicationContext());
songList.add(new Song(thisId, thisTitle, thisArtist, thisPath, thisDuration, thisCover));
}
SongAdapter songAdt = new SongAdapter(getApplicationContext(), songList);
...
我用来获取相册图片封面的代码:
private static String getCoverArtPath(long albumId, Context context) {
Cursor albumCursor = context.getContentResolver().query(
MediaStore.Audio.Albums.EXTERNAL_CONTENT_URI,
new String[]{MediaStore.Audio.Albums.ALBUM_ART},
MediaStore.Audio.Albums._ID + " = ?",
new String[]{Long.toString(albumId)},
null
);
boolean queryResult = albumCursor.moveToFirst();
String result = null;
if (queryResult) {
result = albumCursor.getString(0);
}
albumCursor.close();
return result;
}
Layout AlbumCover:
<ImageView
android:layout_width="50dp"
android:layout_height="50dp"
android:id="@+id/song_cover"
android:scaleType = "fitXY"
android:src="@drawable/note"
android:background="#171717"
android:padding="1dp"
android:layout_marginLeft="3dp" />
适配器中的getView:
@Override
public View getView(View convertView, ViewGroup parent) {
RelativeLayout songLay = (RelativeLayout)songInf.inflate
(R.layout.song_layout, parent, false);
ImageView coverView = (ImageView)songLay.findViewById(R.id.song_cover);
//get song using position
Song currSong = songs.get(position);
if (Drawable.createFromPath(currSong.getCover()) != null) {
Drawable img = Drawable.createFromPath(currSong.getCover());
coverView.setImageDrawable(img);
}
//set position as tag
songLay.setTag(position);
return songLay;
}
修改
我按照建议尝试滑行,但我仍然得到同样的错误:
if (Drawable.createFromPath(currSong.getCover()) != null) {
Drawable img = Drawable.createFromPath(currSong.getCover());
Glide.with(mContext).load("").placeholder(img).override(50,50).into(coverView);
}
这是新错误:
11-11 11:05:55.866 11120-11120/com.xrobot.andrew.musicalbumsE/AndroidRuntime﹕ FATAL EXCEPTION: main
Process: com.xrobot.andrew.musicalbums, PID: 11120
java.lang.OutOfMemoryError: OutOfMemoryError thrown while trying to throw OutOfMemoryError; no stack trace available