Android OutOfMemoryError:?

前端 未结 8 1499
孤街浪徒
孤街浪徒 2020-11-28 06:02

I am sporadically getting an OutOfMemoryError: (Heap Size=49187KB, Allocated=41957KB) in one of my apps. What can I do to diagnose this?

  01-09         


        
8条回答
  •  失恋的感觉
    2020-11-28 06:52

    Before loading images into memory compress your images using

    Bitmap original = BitmapFactory.decodeStream(getAssets().open("1024x768.jpg"));
    ByteArrayOutputStream out = new ByteArrayOutputStream();
    original.compress(Bitmap.CompressFormat.PNG, 100, out);
    Bitmap decoded = BitmapFactory.decodeStream(new ByteArrayInputStream(out.toByteArray()));
    
    Log.e("Original   dimensions", original.getWidth()+" "+original.getHeight());
    Log.e("Compressed dimensions", decoded.getWidth()+" "+decoded.getHeight());  
    

    If you are geting your bitmap from a resource, in which case the bitmap dimension will depend on the phone screen density

    Bitmap bitmap=((BitmapDrawable)getResources().getDrawable(R.drawable.img_1024x768)).getBitmap();
    Log.e("Dimensions", bitmap.getWidth()+" "+bitmap.getHeight());
    

提交回复
热议问题