我正在开发一个 Android 3.1 平板电脑应用程序,我有一个android.support.v4.app.Fragment
带有两个画廊的 Fragment ( )。
我将此代码用作画廊(我对两个画廊使用相同的适配器):
public class ImageGalleryAdapter extends BaseAdapter
{
private ArrayList<String> mImagesPath;
private Context mContext;
private ImageView.ScaleType mScaleType;
private int mWidth;
private int mHeight;
public ArrayList<String> getmImagesPath()
{
return mImagesPath;
}
public void setmImagesPath(ArrayList<String> mImagesPath)
{
this.mImagesPath = mImagesPath;
}
public void addImage(String imagePath)
{
mImagesPath.add(imagePath);
}
public ImageGalleryAdapter(Context context, ImageView.ScaleType scaleType, int width, int height)
{
mContext = context;
mWidth = width;
mHeight = height;
mScaleType = scaleType;
mImagesPath = new ArrayList<String>();
}
@Override
public int getCount()
{
return mImagesPath.size();
}
@Override
public Object getItem(int position)
{
return position;
}
@Override
public long getItemId(int position)
{
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent)
{
// Get a View to display image data
ImageView imageView;
if (convertView == null) { // if it's not recycled, initialize some attributes
imageView = new ImageView(mContext);
imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
imageView.setLayoutParams(new GridView.LayoutParams(
LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT));
} else {
imageView = (ImageView) convertView;
}
File imageFile = new File(mImagesPath.get(position));
if(imageFile.exists())
{
BitmapFactory.Options options = new Options();
options.inSampleSize = 32;
imageView = new ImageView(this.mContext);
Bitmap myBitmap = BitmapFactory.decodeFile(imageFile.getAbsolutePath(), options);
imageView.setImageBitmap(myBitmap);
imageView.setScaleType(mScaleType);
// Set the Width & Height of the individual images
imageView.setLayoutParams(new Gallery.LayoutParams(mWidth, mHeight));
}
return imageView;
}
}
有时,在线路上Bitmap myBitmap = BitmapFactory.decodeFile(imageFile.getAbsolutePath());
,我收到 Out Of Memory 错误
一个画廊有一张图片,另一个画廊有三张图片。
当我转到片段时,会发生内存不足,点击后退按钮,然后我再次回到片段。
我想我需要打电话myBitmap.recycle()
,但在哪里...