[英]Android: outOfMemoryError with bitmaps in Gallery
我有一个应用程序,可以从服务器下载图像,将图像存储在sd卡上,然后以幻灯片形式显示给用户。 该应用程序具有用户可以查看的许多不同的幻灯片。 我的问题是,在查看几个图库后,出现内存不足错误。 我已经做了大量的谷歌搜索工作,并且阅读了大约20遍罗曼·盖伊(Romain Guy)的《避免内存泄漏》一文。 我尝试复制他的unbindDrawables()方法,但无法在Gallery对象上调用butRemoveAllViews()。 我也尝试过在所有位图上调用recycle(),但是当我在适配器内的位图上进行调用时,应用程序在打开图像库后立即引发错误。 我还尝试过重新编码幻灯片,以便在适配器之外创建所有位图,然后将它们作为数组传递给我-这使我可以遍历位图数组,并在幻灯片的onDestroy方法中对每个位图调用recycle()活动-但这实际上似乎使泄漏情况变得越来越糟。
这是我的幻灯片活动的代码:
public class Slideshow extends Activity {
static String galleryId;
public static final int MSG_DOWNLOADED = 0;
static Handler handler;
static Gallery g;
static ArrayList<String> filePaths;
static String subFolder;
static ArrayList<String> imageToGet;
static LinearLayout pb;
static boolean firstTime = true;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.slideshow);
Context context = getApplicationContext();
Bundle extras = getIntent().getExtras();
if(extras != null){
galleryId = extras.getString("galleryId");
}
pb = (LinearLayout) findViewById(R.id.progress);
handler = new Handler(){
@Override
public void handleMessage(Message msg){
switch (msg.what){
case MSG_DOWNLOADED:
g.setAdapter(new SlideshowAdapter(getApplicationContext(), R.id.gallery, filePaths));
pb.setVisibility(8);
if(firstTime){
Thread images = new Thread(){
public void run(){
ImageGallery gallery = XMLParser.getGalleryById(galleryId, false, getApplicationContext());
ArrayList<String> imageURLs = gallery.getImageURLs();
String subFolder = "gallery"+galleryId+"/";
getImages(imageURLs, subFolder, false);
}
};
images.start();
firstTime = false;
}
break;
}
}
};
ImageGallery gallery = XMLParser.getGalleryById(galleryId, false, context);
filePaths = gallery.getFilePaths();
ArrayList<String> imageURLs = gallery.getImageURLs();
subFolder = "gallery"+galleryId+"/";
g = (Gallery) findViewById(R.id.gallery);
if(filePaths.size() > 0){
String filePath = filePaths.get(0);
String url = imageURLs.get(0);
imageToGet = new ArrayList<String>();
imageToGet.add(url);
if(filePaths.size() == 1 ){
File file = new File(filePath);
if(!file.exists()){
Thread getFirstImage = new Thread(){
public void run(){
Log.d("ClubSlideshow getting only image", ""+imageToGet.get(0));
getImages(imageToGet, subFolder, false);
handler.sendEmptyMessage(MSG_DOWNLOADED);
}
};
getFirstImage.start();
}
}else if(filePaths.size()>1){
String filePath2 = filePaths.get(1);
String url2 = imageURLs.get(1);
File file = new File(filePath);
File file2 = new File(filePath2);
imageToGet.add(url2);
if(!file.exists()||!file2.exists()){
Thread getFirstImage = new Thread(){
public void run(){
getImages(imageToGet, subFolder, false);
handler.sendEmptyMessage(MSG_DOWNLOADED);
}
};
getFirstImage.start();
}
}
}
}
public void getImages(ArrayList<String> imageURLs, String subFolder, boolean force){
DataCache.downloadFromUrlArray(imageURLs, subFolder, force, getApplicationContext());
}
public class ImageThread implements Runnable{
public ImageThread(ArrayList<String> imageURLs, String subFolder, Boolean force){
getImages(imageURLs, subFolder, force);
}
public void run(){
}
}
@Override
protected void onDestroy(){
super.onDestroy();
g.setAdapter(null);
}
}
这是我的SlideshowAdapter类的代码:
public class SlideshowAdapter extends ArrayAdapter<String> {
int mGalleryItemBackground;
private Context mContext;
private ArrayList<String> images = new ArrayList<String>();
Bitmap bMap;
String filePath;
File file;
ImageView i;
public SlideshowAdapter(Context c, int resourceId, ArrayList<String> objects){
super(c, resourceId, objects);
this.mContext = c;
this.images = objects;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
if(convertView !=null){
i = (ImageView) convertView;
}else{
i = new ImageView(mContext);
}
filePath = mContext.getFilesDir()+"/"+images.get(position);
file = new File(filePath);
if(file.exists()){
bMap = BitmapFactory.decodeFile(filePath);
i.setImageBitmap(bMap);
}
i.setScaleType(ImageView.ScaleType.FIT_CENTER);
return i;
}
}
谁能看到导致我出现内存问题的原因吗?
实际上有几个问题:
getView
方法convertView为null时,都会看到此错误 您可以尝试使用scaledBitmap减少内存使用量
if(file.exists()){
bMap = BitmapFactory.decodeFile(filePath);
i.setImageBitmap(bMap);
}
至
if(file.exists()){
bMap = BitmapFactory.decodeFile(filePath);
bMap = Bitmap.createScaledBitmap(bMap, 100, 100, true);
i.setImageBitmap(bMap);
}
尝试将图像存储在SD卡或设备内存中,以避免此问题。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.