大侠们,求指点啊,有一张5Mb的图片,我处理完后,仍超内存,下面是我的代码,在线狂等中,雪地里跪求:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Bitmap bm = getimage(path);
image = (ImageView) findViewById(R.id.image);
image.setImageBitmap(bm);
} private Bitmap compressImage(Bitmap image) {
ByteArrayOutputStream baos = new ByteArrayOutputStream();
image.compress(Bitmap.CompressFormat.JPEG, 100, baos);
int options = 100;
while (baos.toByteArray().length / 1024 > 100) {
baos.reset();
image.compress(Bitmap.CompressFormat.JPEG, options, baos);
options -= 10;
}
ByteArrayInputStream isBm = new ByteArrayInputStream(baos.toByteArray());
Bitmap bitmap = BitmapFactory.decodeStream(isBm, null, null);
return bitmap;
} private Bitmap getimage(String srcPath) {
BitmapFactory.Options newOpts = new BitmapFactory.Options();
newOpts.inJustDecodeBounds = true;
Bitmap bitmap = BitmapFactory.decodeFile(srcPath, newOpts); newOpts.inJustDecodeBounds = false;
int w = newOpts.outWidth;
int h = newOpts.outHeight;
float hh = 720f;
float ww = 480f;
int be = 1;
if (w >= h && w >= ww) {
be = (int) (newOpts.outWidth / ww);
} else if (w <= h && h >= hh) {
be = (int) (newOpts.outHeight / hh);
}
if (be <= 0)
be = 1;
newOpts.inSampleSize = be;
bitmap = BitmapFactory.decodeFile(srcPath, newOpts);
return compressImage(bitmap);
}

解决方案 »

  1.   

    LRUCache用过吗?试试
    http://developer.android.com/training/building-graphics.html
      

  2.   

    大侠们,求指点啊,有一张5Mb的图片,我处理完后,仍超内存,下面是我的代码,在线狂等中,雪地里跪求:
    protected void onCreate(Bundle savedInstanceState) {
       super.onCreate(savedInstanceState);
       setContentView(R.layout.main);
       Bitmap bm = getimage(path);
       image = (ImageView) findViewById(R.id.image);
       image.setImageBitmap(bm);
    }private Bitmap compressImage(Bitmap image) {
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        image.compress(Bitmap.CompressFormat.JPEG, 100, baos);
        int options = 100;
        while (baos.toByteArray().length / 1024 > 100) {
        baos.reset();
        image.compress(Bitmap.CompressFormat.JPEG, options, baos);
        options -= 10;
        }
        ByteArrayInputStream isBm = new ByteArrayInputStream(baos.toByteArray());
        Bitmap bitmap = BitmapFactory.decodeStream(isBm, null, null);
        return bitmap;
    }private Bitmap getimage(String srcPath) {
       BitmapFactory.Options newOpts = new BitmapFactory.Options();
       newOpts.inJustDecodeBounds = true;
       Bitmap bitmap = BitmapFactory.decodeFile(srcPath, newOpts);   newOpts.inJustDecodeBounds = false;
       int w = newOpts.outWidth;
       int h = newOpts.outHeight;
       float hh = 720f;
       float ww = 480f;
       int be = 1;
       if (w >= h && w >= ww) {
          be = (int) (newOpts.outWidth / ww);
       } else if (w <= h && h >= hh) {
          be = (int) (newOpts.outHeight / hh);
       }
       if (be <= 0)
         be = 1;
       newOpts.inSampleSize = be;
       bitmap = BitmapFactory.decodeFile(srcPath, newOpts);
       return compressImage(bitmap);
    }
    谁看下上面的代码哪里有问题啊
      

  3.   

    Bitmap的Option也试试改一下,
    BitmapFactory.Options opt = new BitmapFactory.Options();  
        opt.inPreferredConfig = Bitmap.Config.RGB_565;   
        opt.inPurgeable = true;  
        opt.inInputShareable = true;  
    以上之类。
      

  4.   

    当图片太大时,在onCreate()中获得bm.getWidth()和bm.getHeight()时得到的仍是图片处理前的宽和高,在 Bitmap bitmap = BitmapFactory.decodeFile(srcPath, newOpts);这一步提示decode false;但是对于2Mb以下的图片经过处理后得到的bm宽和高就是处理后的了
      

  5.   

    要不参考一下我这个 测试过5M的无压力
    http://download.csdn.net/detail/piaohong/4978208