获取图片的路径。。
private Bitmap getBitMapByPath(String path,int width,int height) throws FileNotFoundException, IOException
{ FileInputStream fis = new FileInputStream(path);
FileDescriptor fd = fis.getFD();
BitmapFactory.Options options = new BitmapFactory.Options();  
options.inSampleSize = 10;
options.inJustDecodeBounds = true;  
BitmapFactory.decodeFileDescriptor(fd, null, options);  
options.inSampleSize = computeSampleSize(options,width,height); //这里调用压缩图片的函数
System.out.println("the inSampleSize is: " + options.inSampleSize);
options.inJustDecodeBounds = false;  
    options.inDither = false;  
    options.inPreferredConfig = Bitmap.Config.ARGB_8888;  
    System.out.println("before decodefile");
    Bitmap sourceBitmap = BitmapFactory.decodeFile(path, options);  //这一行报内存溢出
    

    System.out.println("after decodefile");
    fis.close();
   /* */ System.gc();
    return sourceBitmap;   }压缩函数
private int computeSampleSize(BitmapFactory.Options options, int width,int height) {  
    int w = options.outWidth;  
    int h = options.outHeight;  
    int candidatew = w / width;  
    int candidateh = h / height;
    if(w / width != 0)
    {
     candidatew ++;
    }
    if(h / height != 0)
    {
     candidateh ++;
    }
    int candidate = Math.max(candidatew, candidateh);
    if (candidate == 0)  
    return 1;
    return candidate;  
}  
public void SetImage(String path)
{
try
{     
FrontBitmap = getBitMapByPath(path,panelData.getWidth(),panelData.getHeight());//这里调用了上面第一个函数
if(FrontBitmap != null)
{
FrontScaleBitmap = Bitmap.createScaledBitmap(FrontBitmap, panelData.getWidth(), panelData.getHeight(), true);

/* */if(!FrontBitmap.isRecycled()){//先判断图片是否已释放了   
   
FrontBitmap.recycle();   
/* */System.gc();
   System.out.println("运行了莫");
}  

}
else
{
FrontScaleBitmap = null;
}
/* */if(!FrontBitmap.isRecycled()){//先判断图片是否已释放了   
   
FrontBitmap.recycle();   
   System.out.println("要释放空间1111111111");
}  
// /* */if(!FrontScaleBitmap.isRecycled()){//先判断图片是否已释放了   
//    
// FrontScaleBitmap.recycle();   
//    System.out.println("运行了莫1111111");
// } 

if(imageView == null)
{
AbsoluteLayout.LayoutParams childLp = new AbsoluteLayout.LayoutParams(panelData.getWidth(), panelData.getHeight(), 0, 0);
imageView = new AnimaImageview(mOwnerLayout.getContext()); //new ImageView(mOwnerLayout.getContext());
mOwnerLayout.addView(imageView,childLp);
System.gc();
/* */if(!FrontBitmap.isRecycled()){//先判断图片是否已释放了   
   
FrontBitmap.recycle();   
   System.out.println("要释放空间22222222222");
}  
}
imageView.setImageBitmap(FrontScaleBitmap);

mOwnerLayout.bringChildToFront(imageView);
/* */System.gc();
if(oldFrontBitmap != null)
{
oldFrontBitmap.recycle();
oldFrontBitmap = null;
}
if(oldFrontScaleBitmap != null)
{
oldFrontScaleBitmap.recycle();
oldFrontScaleBitmap = null;
}

oldFrontBitmap = FrontBitmap;
oldFrontScaleBitmap = FrontScaleBitmap;
lastShowDate = Calendar.getInstance().getTime();
}
catch(Throwable te)
{
te.printStackTrace();
}
}
报错提示:
  

解决方案 »

  1.   

    图片资源对于Android的手机应用来说是十分占内存的,所以你应该利用Android开发提供的函数isRecycle()和recycle()来判断和及时释放无用图片占用的的内存资源
      

  2.   

    可以参看gallery2 的压缩方式
      

  3.   

    这段代码肯定有问题的,如下:         private int computeSampleSize(BitmapFactory.Options options, int width,int height) {  
            int w = options.outWidth;  
            int h = options.outHeight;  
            int candidatew = w / width;    = 0
            int candidateh = h / height;   = 0 
            if(w / width != 0)
            {
                candidatew ++;
            }
            if(h / height != 0)  
            {
                candidateh ++;
            }
            int candidate = Math.max(candidatew, candidateh);
            if (candidate == 0)    //错误处:candidate  会始终为0 ,即你的图片没有压缩。 
            return 1;
            return candidate;  
        }    你要使用压缩的话,该如下判断:
    public static int calculateInSampleSize(
                BitmapFactory.Options options, int reqWidth, int reqHeight) {
        // Raw height and width of image
        final int height = options.outHeight;
        final int width = options.outWidth;
        int inSampleSize = 1;    if (height > reqHeight || width > reqWidth) {
            if (width > height) {
                inSampleSize = Math.round((float)height / (float)reqHeight);
            } else {
                inSampleSize = Math.round((float)width / (float)reqWidth);
            }
        }
        return inSampleSize;
    }
     
       具体代码可参考: http://developer.android.com/training/displaying-bitmaps/load-bitmap.html
     
       里面有很多缓存图片,解析图片的例子。
           
      

  4.   

     前面分析有误 , 其实判断问题倒不大,也许是candidate 最大值只能为2,即缩放2倍 。   不知道你的图片到底有多大呢 ?    囧 ~~~~~~~~