这是一个应该看似简单的gallery,在我的emulator上运行没问题的,但是我的同学在手机上运行的时候除了2.3版本系统外都出现了OOM,上网看了很多东西也没找到什么有用的方法(比如在这里我把图像缩小了很多,不过其实这也是治标不治本的方法),有没有可以在每次图片切换的时候释放内存的办法,我试过用.recycle(),不过貌似不行做等求解:
GalleryTest.javapackage lala.test;import java.io.File;
import java.io.FileFilter;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;import android.app.Activity;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.os.Environment;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnTouchListener;
import android.widget.BaseAdapter;
import android.widget.Gallery;
import android.widget.ImageSwitcher;
import android.widget.ImageView;
import android.widget.Gallery.LayoutParams;public class GalleryTest extends Activity { private Gallery gallery;
private List<String> fileNames = null;
private Bitmap bm = null;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Log.d("Main114", "lala"); fileNames = getListOfFiles(); /*
 * Set gallery
 */
gallery = (Gallery) findViewById(R.id.gallery);
gallery.setAdapter(new ImageAdapter(this));
} /*
 * imageview setting for gallery
 */ public View makeView() {
ImageView i = new ImageView(this);
i.setBackgroundColor(0xFF000000);
i.setScaleType(ImageView.ScaleType.FIT_CENTER);
i.setLayoutParams(new ImageSwitcher.LayoutParams(
LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));
return i;
} public class ImageAdapter extends BaseAdapter {
public ImageAdapter(Context c) {
mContext = c;
} public int getCount() {
return fileNames.size();
} public Object getItem(int position) {
return position;
} public long getItemId(int position) {
return position;
} public View getView(int position, View convertView, ViewGroup parent) {
ImageView i = new ImageView(mContext);
BitmapFactory.Options options = new BitmapFactory.Options();
options.inSampleSize = 10; String pathName = Environment.getExternalStorageDirectory()
+ "/BoardTabla/" + fileNames.get(position);
bm = BitmapFactory.decodeFile(pathName, options);
i.setImageBitmap(bm);
return i;
} private Context mContext;
} /**
 * getListOfFiles() get all the filenames of a certain directory
 */ private List<String> getListOfFiles() { File files = new File(Environment.getExternalStorageDirectory()
+ "/BoardTabla/");
FileFilter filter = new FileFilter() {
private final List<String> exts = Arrays.asList("jpeg", "jpg",
"png", "PNG", "JPEG", "JPG"); @Override
public boolean accept(File pathname) {
String ext;
String path = pathname.getPath();
ext = path.substring(path.lastIndexOf(".") + 1);
return exts.contains(ext);
}
}; final File[] filesFound = files.listFiles(filter);
List<String> list = new ArrayList<String>();
if (filesFound != null && filesFound.length > 0) {
for (File file : filesFound) {
list.add(file.getName());
}
}
return list;
}
}main.xml<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"   
    android:layout_width="match_parent"   
    android:layout_height="match_parent">       
      
    <Gallery android:id="@+id/gallery"  
        android:background="#55000000"  
        android:layout_width="match_parent"  
        android:layout_alignParentBottom="true"  
        android:layout_alignParentLeft="true"  
          
        android:gravity="center_vertical"  
        android:spacing="18dp" android:layout_height="match_parent"/>  
</RelativeLayout>