内存不足异常位图

时间:2011-08-08 12:49:00

标签: android memory bitmap

  

可能重复:
  OutOfMemoryError: bitmap size exceeds VM budget :- Android

这是我获取位图的代码。问题是我得到了一个内存不足的例外。我做错了什么:

public void saveImage(byte[] data) {
    BitmapFactory.Options options=new BitmapFactory.Options();
    options.inSampleSize = 8;

    Bitmap bottomImage = BitmapFactory.decodeByteArray(data, 0, data.length);
    Bitmap topImage1 = BitmapFactory.decodeResource(getResources(),R.drawable.mcr);
    Bitmap topImage2 = BitmapFactory.decodeResource(getResources(),R.drawable.blink);
    Bitmap topImage3 = BitmapFactory.decodeResource(getResources(),R.drawable.rvb);
    Bitmap topImage4 = BitmapFactory.decodeResource(getResources(),R.drawable.surfrider);

    int w = topImage1.getWidth();
    int h = topImage1.getHeight(); 
    int bw= bottomImage.getWidth();
    int bh= bottomImage.getHeight();
    //rotate and scale
    int newWidth=960;
    int newHeight=640;
    float scaleWidth = ((float) newWidth) / bw;
    float scaleHeight = ((float) newHeight) / bh;
    Matrix matrix = new Matrix();
    // resize the bit map
    matrix.postScale(scaleWidth, scaleHeight);

    Bitmap resizedBitmap =getResizedBitmap(bottomImage, 640, 960);


    //end rotate and scale
    Bitmap new_image_t1 = topImage1.copy(Bitmap.Config.ARGB_8888, true);
    Bitmap new_image_t2 = topImage2.copy(Bitmap.Config.ARGB_8888, true);
    Bitmap new_image_t3 = topImage3.copy(Bitmap.Config.ARGB_8888, true);
    Bitmap new_image_t4 = topImage4.copy(Bitmap.Config.ARGB_8888, true);
    Log.v(Integer.toString(resizedBitmap.getWidth()),"image size");

   FileOutputStream  os;
   Canvas comboImage = new Canvas(resizedBitmap);

    // Then draw the second on top of that
   comboImage.drawBitmap(new_image_t1, 0f, 0f, null);


   try {

    os= openFileOutput("image1.jpg", Context.MODE_WORLD_READABLE);
    resizedBitmap.compress(CompressFormat.PNG, 100, os);

   } catch(IOException e) {
    Log.v("error saving","error saving");
       e.printStackTrace();
   }
   comboImage.drawBitmap(new_image_t2, 0f, 0f, null);
   try {
        os= openFileOutput("image2.jpg", Context.MODE_WORLD_READABLE);
        resizedBitmap.compress(CompressFormat.PNG, 100, os);

      } catch(IOException e) {
        Log.v("error saving","error saving");
          e.printStackTrace();
      }
   comboImage.drawBitmap(new_image_t3, 0f, 0f, null);
   try {
        //String root = Environment.gedaptExternalStorageDirectory().toString();
        //new File(root + "/myImages").mkdirs();
        os= openFileOutput("image3.jpg", Context.MODE_WORLD_READABLE);
        resizedBitmap.compress(CompressFormat.PNG, 100, os);

      } catch(IOException e) {
        Log.v("error saving","error saving");
          e.printStackTrace();
      }
   comboImage.drawBitmap(new_image_t4, 0f, 0f, null);
   try {

        os= openFileOutput("image4.jpg", Context.MODE_WORLD_READABLE);
        resizedBitmap.compress(CompressFormat.PNG, 100, os);

      } catch(IOException e) {
        Log.v("error saving","error saving");
          e.printStackTrace();
      }
}

感谢。

0 个答案:

没有答案