应用程序崩溃时出现错误
08-27 23:32:33.714 1178-1223/com.statusstock.wallpaperhd I/art: Clamp target GC heap from 398MB to 384MB
08-27 23:32:33.715 1178-1223/com.statusstock.wallpaperhd I/art: Alloc concurrent mark sweep GC freed 9(288B) AllocSpace objects, 0(0B) LOS objects, 0% free, 382MB/384MB, paused 533us total 58.639ms
Forcing collection of SoftReferences for 3MB allocation
Starting a blocking GC Alloc
08-27 23:32:33.766 1178-1223/com.statusstock.wallpaperhd I/art: Clamp target GC heap from 398MB to 384MB
Alloc concurrent mark sweep GC freed 3(96B) AllocSpace objects, 0(0B) LOS objects, 0% free, 382MB/384MB, paused 439us total 51.432ms
08-27 23:32:33.766 1178-1223/com.statusstock.wallpaperhd W/art: Throwing OutOfMemoryError "Failed to allocate a 3686412 byte allocation with 1976816 free bytes and 1930KB until OOM"
08-27 23:32:33.766 1178-1223/com.statusstock.wallpaperhd D/skia: --- decoder->decode returned false
这是一个墙纸应用程序,它通过从Firebase检索图像URL在Picasso的帮助下以网格视图显示图像。如果单击图像,它将在Picasso和firebase的帮助下在新活动中打开。但是,当我运行我的应用程序时,它占用了太多内存,并且使用几分钟后,应用程序崩溃了。我已经使用了android:hardwareAccelerated =“ false”和android:largeHeap =“ true”,但它们无法正常工作。
代码:
Viewholder.java
import ...
public class ViewHolder extends RecyclerView.ViewHolder {
View mView;
public ViewHolder(final View itemView) {
super(itemView);
mView = itemView;
itemView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
mClickListener.onItemClick(v, getAdapterPosition());
}
});
itemView.setOnLongClickListener(new View.OnLongClickListener() {
@Override
public boolean onLongClick(View v) {
mClickListener.onItemLongClick(v, getAdapterPosition());
return true;
}
});
}
public void setDetails(Context ctx, String image){
ImageView mImageTv = mView.findViewById(R.id.rImageView);
Picasso.get().load(image).into(mImageTv);
}
private ViewHolder.ClickListener mClickListener;
public interface ClickListener{
void onItemClick(View view, int position);
void onItemLongClick(View view, int position);
}
public void setOnclickListener(ViewHolder.ClickListener clickListener){
mClickListener = clickListener;
}
}
在网格视图中显示墙纸的片段
public class HDFragment extends Fragment {
RecyclerView mRecyclerView;
FirebaseDatabase mFirebaseDatabase;
DatabaseReference mRef;
@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_hd, container, false);
mRecyclerView = view.findViewById(R.id.recyclerView_hd);
mRecyclerView.setHasFixedSize(true);
mRecyclerView.setLayoutManager(new GridLayoutManager(getActivity(), 3));
mFirebaseDatabase = FirebaseDatabase.getInstance();
mRef = mFirebaseDatabase.getReference("New");
FirebaseRecyclerAdapter<Model, ViewHolder> firebaseRecyclerAdapter = new FirebaseRecyclerAdapter<Model, ViewHolder>(Model.class, R.layout.row, ViewHolder.class, mRef) {
@Override
protected void populateViewHolder(ViewHolder viewHolder, Model model, int position) {
viewHolder.setDetails(getActivity().getApplicationContext(), model.getImage());
}
@Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
ViewHolder viewHolder = super.onCreateViewHolder(parent, viewType);
viewHolder.setOnclickListener(new ViewHolder.ClickListener() {
@Override
public void onItemClick(View view, int position) {
String mImage = getItem(position).getImage();
Intent intent = new Intent(view.getContext(), PostDetailsActivity2.class);
intent.putExtra("image", mImage);
startActivity(intent);
}
@Override
public void onItemLongClick(View view, int position) {
}
});
return viewHolder;
}
};
mRecyclerView.setAdapter(firebaseRecyclerAdapter);
return view;
}
}
FullImage活动在单击时会全屏显示图像
mImageTv = findViewById(R.id.full_imageView1);
String image = getIntent().getStringExtra("image");
Picasso.get().load(image).fit().centerCrop().memoryPolicy(MemoryPolicy.NO_CACHE).into(mImageTv);
请帮助。。谢谢
答案 0 :(得分:0)
如果您确实需要更多内存,可以在“清单”中为Application设置此属性,以解决问题(也许):
android:largeHeap="true"
尽管它不是免费提供的,您可以在这篇文章中阅读: What are advantages of setting largeHeap to true?
答案 1 :(得分:0)
您正在尝试加载大图像,降低图像分辨率或使用可处理大图像的视图,例如this。