setImageResource总是给出OutOfMemoryError

问题描述 投票:-1回答:2

我正在尝试使用card_layout创建一个ListView,它接受ImageView和3个TextViews:

<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="80dp"
android:orientation="horizontal">
<ImageView
    android:id="@+id/book_cover"
    android:layout_width="0dp"
    android:layout_height="match_parent"
    android:layout_weight="2"/>
<LinearLayout
    android:layout_width="0dp"
    android:layout_height="match_parent"
    android:layout_weight="8"
    android:orientation="vertical">
    <TextView
        android:id="@+id/book_author"
        style="@style/subheading_style"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"/>
    <TextView
        android:id="@+id/book_title"
        style="@style/heading_style"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"/>
    <TextView
        android:id="@+id/book_completion_status"
        style="@style/third_heading_style"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"/>
</LinearLayout>

使用CardItem对象填充卡片

public class CardItem
{
private String bookAuthor;
private String bookTitle;
private String bookCompletionStatus;
private int bookCoverResID;

/*
* Constructors
*/
public CardItem(String bookTitle)
{
    this.bookTitle = bookTitle;
    bookCoverResID = 0;
}

public CardItem(String bookTitle, String bookAuthor, String bookCompletionStatus)
{
    this.bookAuthor = bookAuthor;
    this.bookTitle = bookTitle;
    this.bookCompletionStatus = bookCompletionStatus;
    bookCoverResID = 0;
}

public CardItem(String bookTitle, String bookAuthor, String bookCompletionStatus, int bookCoverResID)
{
    this.bookAuthor = bookAuthor;
    this.bookTitle = bookTitle;
    this.bookCompletionStatus = bookCompletionStatus;
    this.bookCoverResID = bookCoverResID;
}

/*
* Getters and Setters
*/
public String getBookAuthor() {
    return bookAuthor;
}

public void setBookAuthor(String bookAuthor) {
    this.bookAuthor = bookAuthor;
}

public String getBookTitle() {
    return bookTitle;
}

public void setBookTitle(String bookTitle) {
    this.bookTitle = bookTitle;
}

public int getBookCoverResID() {
    return bookCoverResID;
}

public void setBookCoverResID(int bookCoverResID) {
    this.bookCoverResID = bookCoverResID;
}

public String getBookCompletionStatus() {
    return bookCompletionStatus;
}

public void setBookCompletionStatus(String bookCompletionStatus) {
    this.bookCompletionStatus = bookCompletionStatus;
}

/*
* misc. methods
*/
public boolean hasBookCover()
{
    return (bookCoverResID!=0);
}
}

并使用CardItemAdapter填充listview

public class CardItemAdapter extends ArrayAdapter<CardItem>
{
public CardItemAdapter(@NonNull Context context, int resource, @NonNull List<CardItem> objects) {
    super(context, resource, objects);
}

@NonNull
@Override
public View getView(int position, @Nullable View convertView, @NonNull ViewGroup parent)
{
    View tempView = convertView;
    if(tempView == null)
        tempView = LayoutInflater.from(getContext()).inflate( R.layout.card_layout, parent, false);

    TextView bookAuthor = tempView.findViewById(R.id.book_author);
    TextView bookTitle = tempView.findViewById(R.id.book_title);
    TextView bookCompletionStatus = tempView.findViewById(R.id.book_completion_status);
    ImageView bookCover = tempView.findViewById(R.id.book_cover);

    bookTitle.setText(getItem(position).getBookTitle());

    if(bookAuthor != null)
        bookAuthor.setText(getItem(position).getBookAuthor());

    if(bookCompletionStatus != null)
        bookCompletionStatus.setText(getItem(position).getBookCompletionStatus());


    if(getItem(position).hasBookCover())
        bookCover.setImageResource(getItem(position).getBookCoverResID());
    else
        bookCover.setVisibility(View.GONE);

    return tempView;
}
}

这是onCreate中MainActivity的代码

ListView listView;
ArrayList<CardItem> cardItems = new ArrayList<>();
cardItems.add(new CardItem("Origin", "Dan Brown", "Completed"));
    cardItems.add(new CardItem("A Column of Fire"));
    cardItems.add(new CardItem("People vs Alex Cross", "James Patterson",
            "Completed", R.raw.people_vs_alex_cross));

    listView = findViewById(R.id.list_view);
    listView.setAdapter(new CardItemAdapter(this, R.layout.activity_main, cardItems));

Android应用程序总是在启动时崩溃,在setImageResourceID中的适配器的getView()方法中给出了OutOfMemoryError。该应用程序工作正常,没有if / else语句。我使用的图像是存储在res \ raw文件夹中的.jpg文件。

android out-of-memory
2个回答
0
投票

res/raw/中的图像行为没有很好的记录。我建议将图像放在可绘制的资源目录中。如果要在所有屏幕密度上使用相同的图像,请使用res/drawable-anydpi/

除此之外,请确保您的图像具有合理的分辨率。图像使用的内存量是分辨率的函数,而不是磁盘大小。例如,1536 x 2344像素图像将消耗4 * 1536 * 2344字节= 14,401,536字节= ~14MB。您不太可能拥有大的连续空闲内存块。


0
投票

android:largeHeap=true

在清单应用标签中

© www.soinside.com 2019 - 2024. All rights reserved.