使用 setImageURI 设置 Uri 时出现 OutOfMemoryError
OutOfMemoryError when setting Uri with setImageURI
用户select一张图片,图片的路径然后存储在我的SQLite数据库中。然后我使用 CursorAdapter:
在 GridView
中从 SQLite 填充 text/images
public class MyNiftyAdapter extends CursorAdapter{
private LayoutInflater mInflater;
private Cursor cur;
public MyNiftyAdapter(Context context, Cursor c) {
super(context,c);
this.mInflater = LayoutInflater.from(context);
this.cur = c;
}
public MyNiftyAdapter(Context context, Cursor c, boolean autoRequery)
{
super(context, c, autoRequery);
this.mInflater = LayoutInflater.from(context);
this.cur = c;
}
@Override
public View getView(int position, View convertView, ViewGroup parent)
{
ViewHolder viewHolder;
if(convertView == null)
{
convertView = this.mInflater.inflate(R.layout.single_item, null);
viewHolder = new ViewHolder();
viewHolder.name = (TextView)convertView.findViewById(R.id.txtName);
viewHolder.Age = (TextView)convertView.findViewById(R.id.studentage);
viewHolder.Id = (TextView)convertView.findViewById(R.id.rowid);
viewHolder.Image = (CircularImageView)convertView.findViewById(R.id.imgFood);
convertView.setTag(viewHolder);
}else
{
viewHolder = (ViewHolder)convertView.getTag();
}
this.cur.moveToPosition(position);
viewHolder.name.setText(this.cur.getString(this.cur.getColumnIndex(SQLiteHelper.NAME)));
viewHolder.Age.setText(this.cur.getString(this.cur.getColumnIndex(SQLiteHelper.AGE)));
viewHolder.Id.setText(this.cur.getString(this.cur.getColumnIndex(SQLiteHelper._ID)));
Uri jg = Uri.parse(this.cur.getString(this.cur.getColumnIndex("imagepath")));
viewHolder.Image.setImageURI(jg);
return convertView;
}
@Override
public void bindView(View view, Context context, Cursor cursor) {
}
@Override
public View newView(Context context, Cursor cursor, ViewGroup parent) {
// Dont need to do anything here
return null;
}
static class ViewHolder
{
TextView name;
TextView Age;
TextView Id;
CircularImageView Image;
}
}
当我 select 大图像时,出现内存不足错误:
java.lang.OutOfMemoryError: Failed to allocate a 21566748 byte allocation with 8448604 free bytes and 8MB until OOM
我看到位图应该按比例缩小,但我只是使用文件的 Uri
并显示它。有没有办法避免这个问题,因为我不想将另一个(压缩的)文件保存到设备?
我已经在清单中尝试 android:hardwareAccelerated="false"
。
这对我有用:
我最终使用 picasso 作为建议的答案,但需要注意的重要一点是,当从设备 加载 URI
时,加载URI
像这样:
Picasso.with(mContext)
.load(new File(String.valueOf(Uri)))
.placeholder(R.drawable.profile)
.resize(800, 800)
.centerCrop()
.into(viewHolder.Image);
请注意,我使用 .load(new File(String.valueOf(Uri)))
而不是 .load(uri)
。
如果直接加载 URI
,视图将 return 为空。
如果您从 url 加载 图像,它应该像这样加载:
Picasso.with(mContext)
.load(URL)
.placeholder(R.drawable.profile)
.resize(800, 800)
.centerCrop()
.into(viewHolder.Image);
所以我的适配器最终看起来像这样:
public class MyNiftyAdapter extends CursorAdapter{
private LayoutInflater mInflater;
private Cursor cur;
private Context mContext;
public MyNiftyAdapter(Context context, Cursor c) {
super(context,c);
this.mInflater = LayoutInflater.from(context);
this.mContext = context;
this.cur = c;
}
public MyNiftyAdapter(Context context, Cursor c, boolean autoRequery)
{
super(context, c, autoRequery);
this.mInflater = LayoutInflater.from(context);
this.cur = c;
this.mContext = context;
}
@Override
public View getView(int position, View convertView, ViewGroup parent)
{
ViewHolder viewHolder;
if(convertView == null)
{
convertView = this.mInflater.inflate(R.layout.single_item, null);
viewHolder = new ViewHolder();
viewHolder.name = (TextView)convertView.findViewById(R.id.txtName);
viewHolder.Age = (TextView)convertView.findViewById(R.id.studentage);
viewHolder.Id = (TextView)convertView.findViewById(R.id.rowid);
viewHolder.Image = (CircularImageView)convertView.findViewById(R.id.imgFood);
convertView.setTag(viewHolder);
}else
{
viewHolder = (ViewHolder)convertView.getTag();
}
this.cur.moveToPosition(position);
viewHolder.name.setText(this.cur.getString(this.cur.getColumnIndex(SQLiteHelper.NAME)));
viewHolder.Age.setText(this.cur.getString(this.cur.getColumnIndex(SQLiteHelper.AGE)));
viewHolder.Id.setText(this.cur.getString(this.cur.getColumnIndex(SQLiteHelper._ID)));
Uri jg = Uri.parse(this.cur.getString(this.cur.getColumnIndex("imagepath")));
Picasso.with(mContext)
.load(new File(String.valueOf(jg)))
.placeholder(R.drawable.profile)
.resize(800, 800)
.centerCrop()
.into(viewHolder.Image);
return convertView;
}
@Override
public void bindView(View view, Context context, Cursor cursor) {
}
@Override
public View newView(Context context, Cursor cursor, ViewGroup parent) {
// Dont need to do anything here
return null;
}
static class ViewHolder
{
TextView name;
TextView Age;
TextView Id;
CircularImageView Image;
}
}
谢谢大家的帮助。
您可以使用 Picasso 库来显示图像。
compile 'com.squareup.picasso:picasso:2.5.2'
在你的适配器中是这样的:
Uri jg = Uri.parse(this.cur.getString(this.cur.getColumnIndex("imagepath")));
Picasso.with(context).load(jg).into(viewHolder.Image);
对于上下文
public class MyNiftyAdapter extends CursorAdapter{
private LayoutInflater mInflater;
private Cursor cur;
private Context mContext;
public MyNiftyAdapter(Context context, Cursor c) {
super(context,c);
this.mInflater = LayoutInflater.from(context);
this.mContext= context;
this.cur = c;
}
public MyNiftyAdapter(Context context, Cursor c, boolean autoRequery)
{
super(context, c, autoRequery);
this.mInflater = LayoutInflater.from(context);
this.cur = c;
this.mContext= context;
}
然后使用:
Uri jg = Uri.parse(this.cur.getString(this.cur.getColumnIndex("imagepath")));
Picasso.with(mContext).load(jg).into(viewHolder.Image);
用户select一张图片,图片的路径然后存储在我的SQLite数据库中。然后我使用 CursorAdapter:
在GridView
中从 SQLite 填充 text/images
public class MyNiftyAdapter extends CursorAdapter{
private LayoutInflater mInflater;
private Cursor cur;
public MyNiftyAdapter(Context context, Cursor c) {
super(context,c);
this.mInflater = LayoutInflater.from(context);
this.cur = c;
}
public MyNiftyAdapter(Context context, Cursor c, boolean autoRequery)
{
super(context, c, autoRequery);
this.mInflater = LayoutInflater.from(context);
this.cur = c;
}
@Override
public View getView(int position, View convertView, ViewGroup parent)
{
ViewHolder viewHolder;
if(convertView == null)
{
convertView = this.mInflater.inflate(R.layout.single_item, null);
viewHolder = new ViewHolder();
viewHolder.name = (TextView)convertView.findViewById(R.id.txtName);
viewHolder.Age = (TextView)convertView.findViewById(R.id.studentage);
viewHolder.Id = (TextView)convertView.findViewById(R.id.rowid);
viewHolder.Image = (CircularImageView)convertView.findViewById(R.id.imgFood);
convertView.setTag(viewHolder);
}else
{
viewHolder = (ViewHolder)convertView.getTag();
}
this.cur.moveToPosition(position);
viewHolder.name.setText(this.cur.getString(this.cur.getColumnIndex(SQLiteHelper.NAME)));
viewHolder.Age.setText(this.cur.getString(this.cur.getColumnIndex(SQLiteHelper.AGE)));
viewHolder.Id.setText(this.cur.getString(this.cur.getColumnIndex(SQLiteHelper._ID)));
Uri jg = Uri.parse(this.cur.getString(this.cur.getColumnIndex("imagepath")));
viewHolder.Image.setImageURI(jg);
return convertView;
}
@Override
public void bindView(View view, Context context, Cursor cursor) {
}
@Override
public View newView(Context context, Cursor cursor, ViewGroup parent) {
// Dont need to do anything here
return null;
}
static class ViewHolder
{
TextView name;
TextView Age;
TextView Id;
CircularImageView Image;
}
}
当我 select 大图像时,出现内存不足错误:
java.lang.OutOfMemoryError: Failed to allocate a 21566748 byte allocation with 8448604 free bytes and 8MB until OOM
我看到位图应该按比例缩小,但我只是使用文件的 Uri
并显示它。有没有办法避免这个问题,因为我不想将另一个(压缩的)文件保存到设备?
我已经在清单中尝试 android:hardwareAccelerated="false"
。
这对我有用:
我最终使用 picasso 作为建议的答案,但需要注意的重要一点是,当从设备 加载 URI
时,加载URI
像这样:
Picasso.with(mContext)
.load(new File(String.valueOf(Uri)))
.placeholder(R.drawable.profile)
.resize(800, 800)
.centerCrop()
.into(viewHolder.Image);
请注意,我使用 .load(new File(String.valueOf(Uri)))
而不是 .load(uri)
。
如果直接加载 URI
,视图将 return 为空。
如果您从 url 加载 图像,它应该像这样加载:
Picasso.with(mContext)
.load(URL)
.placeholder(R.drawable.profile)
.resize(800, 800)
.centerCrop()
.into(viewHolder.Image);
所以我的适配器最终看起来像这样:
public class MyNiftyAdapter extends CursorAdapter{
private LayoutInflater mInflater;
private Cursor cur;
private Context mContext;
public MyNiftyAdapter(Context context, Cursor c) {
super(context,c);
this.mInflater = LayoutInflater.from(context);
this.mContext = context;
this.cur = c;
}
public MyNiftyAdapter(Context context, Cursor c, boolean autoRequery)
{
super(context, c, autoRequery);
this.mInflater = LayoutInflater.from(context);
this.cur = c;
this.mContext = context;
}
@Override
public View getView(int position, View convertView, ViewGroup parent)
{
ViewHolder viewHolder;
if(convertView == null)
{
convertView = this.mInflater.inflate(R.layout.single_item, null);
viewHolder = new ViewHolder();
viewHolder.name = (TextView)convertView.findViewById(R.id.txtName);
viewHolder.Age = (TextView)convertView.findViewById(R.id.studentage);
viewHolder.Id = (TextView)convertView.findViewById(R.id.rowid);
viewHolder.Image = (CircularImageView)convertView.findViewById(R.id.imgFood);
convertView.setTag(viewHolder);
}else
{
viewHolder = (ViewHolder)convertView.getTag();
}
this.cur.moveToPosition(position);
viewHolder.name.setText(this.cur.getString(this.cur.getColumnIndex(SQLiteHelper.NAME)));
viewHolder.Age.setText(this.cur.getString(this.cur.getColumnIndex(SQLiteHelper.AGE)));
viewHolder.Id.setText(this.cur.getString(this.cur.getColumnIndex(SQLiteHelper._ID)));
Uri jg = Uri.parse(this.cur.getString(this.cur.getColumnIndex("imagepath")));
Picasso.with(mContext)
.load(new File(String.valueOf(jg)))
.placeholder(R.drawable.profile)
.resize(800, 800)
.centerCrop()
.into(viewHolder.Image);
return convertView;
}
@Override
public void bindView(View view, Context context, Cursor cursor) {
}
@Override
public View newView(Context context, Cursor cursor, ViewGroup parent) {
// Dont need to do anything here
return null;
}
static class ViewHolder
{
TextView name;
TextView Age;
TextView Id;
CircularImageView Image;
}
}
谢谢大家的帮助。
您可以使用 Picasso 库来显示图像。
compile 'com.squareup.picasso:picasso:2.5.2'
在你的适配器中是这样的:
Uri jg = Uri.parse(this.cur.getString(this.cur.getColumnIndex("imagepath")));
Picasso.with(context).load(jg).into(viewHolder.Image);
对于上下文
public class MyNiftyAdapter extends CursorAdapter{
private LayoutInflater mInflater;
private Cursor cur;
private Context mContext;
public MyNiftyAdapter(Context context, Cursor c) {
super(context,c);
this.mInflater = LayoutInflater.from(context);
this.mContext= context;
this.cur = c;
}
public MyNiftyAdapter(Context context, Cursor c, boolean autoRequery)
{
super(context, c, autoRequery);
this.mInflater = LayoutInflater.from(context);
this.cur = c;
this.mContext= context;
}
然后使用:
Uri jg = Uri.parse(this.cur.getString(this.cur.getColumnIndex("imagepath")));
Picasso.with(mContext).load(jg).into(viewHolder.Image);