Android如何解决因使用大量图片而导致的内存泄露问题?
3个回答
展开全部
Android分配给图片但内存只有8M,所以当很多图片使用但时候就设计到了内存泄漏问题,网上只是简单但说了要recycle()一下,那是在使用Bitmap的时候,那么如果使用图片的时候没有涉及到Bitmap呢?
下面的是一个程序,可以运行,但是如果图片较多会出现内存泄漏,请问怎么解决呢
我用的是ubuntu11.04运行出了这个问题,但是用手机测试以及windows没有问题。不过因使用大量图片引起的内存泄露的确是个问题,所以要解决它才能以防后患,希望各位高手能够解答。
博客园的农民伯伯说是:写一个全局的资源管理类,用Map和软引用来保存这些图片资源,而不是直接引用资源文件
但是还没有理解,如果有可以解决的跟大家分享一下哈
Java代码
package com.loulijun.imageswitcher;
import android.app.Activity;
import android.content.Context;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewGroup.LayoutParams;
import android.view.Window;
import android.view.animation.AnimationUtils;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.Gallery;
import android.widget.ImageSwitcher;
import android.widget.ImageView;
import android.widget.ViewSwitcher.ViewFactory;
public class ImageSwitcherActivity extends Activity implements ViewFactory {
private ImageSwitcher is;
private Gallery gallery;
//图片资源
private Integer[] thumbs = {
R.drawable.yuanyuan01,
R.drawable.yuanyuan02,
R.drawable.yuanyuan03,
R.drawable.yuanyuan04,
R.drawable.yuanyuan05,
R.drawable.yuanyuan06,
R.drawable.yuanyuan07,
R.drawable.yuanyuan08
};
private Integer[] imgIds = {
R.drawable.yuanyuan01,
R.drawable.yuanyuan02,
R.drawable.yuanyuan03,
R.drawable.yuanyuan04,
R.drawable.yuanyuan05,
R.drawable.yuanyuan06,
R.drawable.yuanyuan07,
R.drawable.yuanyuan08
};
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//设置为全屏模式
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.main);
is = (ImageSwitcher)findViewById(R.id.switcher);
is.setFactory(this);
//淡入淡出
is.setInAnimation(AnimationUtils.loadAnimation(this, android.R.anim.fade_in));
is.setOutAnimation(AnimationUtils.loadAnimation(this, android.R.anim.fade_out));
gallery = (Gallery)findViewById(R.id.gallery);
gallery.setAdapter(new ImageAdapter(this));
gallery.setOnItemSelectedListener(new Gallery.OnItemSelectedListener()
{
public void onItemSelected(AdapterView<?> arg0, View arg1,
int arg2, long arg3) {
is.setImageResource(imgIds[arg2]);
}
public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub
}
});
}
public View makeView() {
ImageView i = new ImageView(this);
i.setBackgroundColor(0xFF000000);
i.setScaleType(ImageView.ScaleType.FIT_CENTER);
i.setLayoutParams(new ImageSwitcher.LayoutParams(
LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT));
return i;
}
public class ImageAdapter extends BaseAdapter
{
//重写构造方法
private Context context;
public ImageAdapter(Context c)
{
context = c;
}
//返回图片的个数
public int getCount() {
// TODO Auto-generated method stub
return thumbs.length;
}
public Object getItem(int position) {
// TODO Auto-generated method stub
return position;
}
public long getItemId(int position) {
// TODO Auto-generated method stub
return position;
}
public View getView(int position, View convertView, ViewGroup parent) {
ImageView iv = new ImageView(context);
iv.setImageResource(thumbs[position]);
iv.setAdjustViewBounds(true);
iv.setLayoutParams(new Gallery.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
return iv;
}
}
}
下面的是一个程序,可以运行,但是如果图片较多会出现内存泄漏,请问怎么解决呢
我用的是ubuntu11.04运行出了这个问题,但是用手机测试以及windows没有问题。不过因使用大量图片引起的内存泄露的确是个问题,所以要解决它才能以防后患,希望各位高手能够解答。
博客园的农民伯伯说是:写一个全局的资源管理类,用Map和软引用来保存这些图片资源,而不是直接引用资源文件
但是还没有理解,如果有可以解决的跟大家分享一下哈
Java代码
package com.loulijun.imageswitcher;
import android.app.Activity;
import android.content.Context;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewGroup.LayoutParams;
import android.view.Window;
import android.view.animation.AnimationUtils;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.Gallery;
import android.widget.ImageSwitcher;
import android.widget.ImageView;
import android.widget.ViewSwitcher.ViewFactory;
public class ImageSwitcherActivity extends Activity implements ViewFactory {
private ImageSwitcher is;
private Gallery gallery;
//图片资源
private Integer[] thumbs = {
R.drawable.yuanyuan01,
R.drawable.yuanyuan02,
R.drawable.yuanyuan03,
R.drawable.yuanyuan04,
R.drawable.yuanyuan05,
R.drawable.yuanyuan06,
R.drawable.yuanyuan07,
R.drawable.yuanyuan08
};
private Integer[] imgIds = {
R.drawable.yuanyuan01,
R.drawable.yuanyuan02,
R.drawable.yuanyuan03,
R.drawable.yuanyuan04,
R.drawable.yuanyuan05,
R.drawable.yuanyuan06,
R.drawable.yuanyuan07,
R.drawable.yuanyuan08
};
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//设置为全屏模式
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.main);
is = (ImageSwitcher)findViewById(R.id.switcher);
is.setFactory(this);
//淡入淡出
is.setInAnimation(AnimationUtils.loadAnimation(this, android.R.anim.fade_in));
is.setOutAnimation(AnimationUtils.loadAnimation(this, android.R.anim.fade_out));
gallery = (Gallery)findViewById(R.id.gallery);
gallery.setAdapter(new ImageAdapter(this));
gallery.setOnItemSelectedListener(new Gallery.OnItemSelectedListener()
{
public void onItemSelected(AdapterView<?> arg0, View arg1,
int arg2, long arg3) {
is.setImageResource(imgIds[arg2]);
}
public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub
}
});
}
public View makeView() {
ImageView i = new ImageView(this);
i.setBackgroundColor(0xFF000000);
i.setScaleType(ImageView.ScaleType.FIT_CENTER);
i.setLayoutParams(new ImageSwitcher.LayoutParams(
LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT));
return i;
}
public class ImageAdapter extends BaseAdapter
{
//重写构造方法
private Context context;
public ImageAdapter(Context c)
{
context = c;
}
//返回图片的个数
public int getCount() {
// TODO Auto-generated method stub
return thumbs.length;
}
public Object getItem(int position) {
// TODO Auto-generated method stub
return position;
}
public long getItemId(int position) {
// TODO Auto-generated method stub
return position;
}
public View getView(int position, View convertView, ViewGroup parent) {
ImageView iv = new ImageView(context);
iv.setImageResource(thumbs[position]);
iv.setAdjustViewBounds(true);
iv.setLayoutParams(new Gallery.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
return iv;
}
}
}
展开全部
不过因使用大量图片引起的内存泄露的确是个问题,所以要解决它才能以防后患,希望各位高手能够解答。
package com.loulijun.imageswitcher; import android.app.Activity; import android.content.Context; import android.graphics.Bitmap; import android.os.Bundle; import android.view.View; import android.view.ViewGroup; import android.view.ViewGroup.LayoutParams; import android.view.Window; import android.view.animation.AnimationUtils; import android.widget.AdapterView; import android.widget.BaseAdapter; import android.widget.Gallery; import android.widget.ImageSwitcher; import android.widget.ImageView; import android.widget.ViewSwitcher.ViewFactory; public class ImageSwitcherActivity extends Activity implements ViewFactory { private ImageSwitcher is; private Gallery gallery; //图片资源 private Integer[] thumbs = { R.drawable.yuanyuan01, R.drawable.yuanyuan02, R.drawable.yuanyuan03, R.drawable.yuanyuan04, R.drawable.yuanyuan05, R.drawable.yuanyuan06, R.drawable.yuanyuan07, R.drawable.yuanyuan08 }; private Integer[] imgIds = { R.drawable.yuanyuan01, R.drawable.yuanyuan02, R.drawable.yuanyuan03, R.drawable.yuanyuan04, R.drawable.yuanyuan05, R.drawable.yuanyuan06, R.drawable.yuanyuan07, R.drawable.yuanyuan08 }; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); //设置为全屏模式 requestWindowFeature(Window.FEATURE_NO_TITLE); setContentView(R.layout.main); is = (ImageSwitcher)findViewById(R.id.switcher); is.setFactory(this); //淡入淡出 is.setInAnimation(AnimationUtils.loadAnimation(this, android.R.anim.fade_in)); is.setOutAnimation(AnimationUtils.loadAnimation(this, android.R.anim.fade_out)); gallery = (Gallery)findViewById(R.id.gallery); gallery.setAdapter(new ImageAdapter(this)); gallery.setOnItemSelectedListener(new Gallery.OnItemSelectedListener() { public void onItemSelected(AdapterView arg0) { // TODO Auto-generated method stub } }); } public View makeView() { ImageView i = new ImageView(this); i.setBackgroundColor(0xFF000000); i.setScaleType(ImageView.ScaleType.FIT_CENTER); i.setLayoutParams(new ImageSwitcher.LayoutParams( LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT)); return i; } public class ImageAdapter extends BaseAdapter { //重写构造方法 private Context context; public ImageAdapter(Context c) { context = c; } //返回图片的个数 public int getCount() { // TODO Auto-generated method stub return thumbs.length; } public Object getItem(int position) { // TODO Auto-generated method stub return position; } public long getItemId(int position) { // TODO Auto-generated method stub return position; } public View getView(int position, View convertView, ViewGroup parent) { ImageView iv = new ImageView(context); iv.setImageResource(thumbs[position]); iv.setAdjustViewBounds(true); iv.setLayoutParams(new Gallery.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT)); return iv; } } }
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
可以下载一个百度云,然后把图片保存到百度云里,很方便的,还可以节省大量空间
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询