High Resolution Image - OutOfMemoryError

I am developing an application for the Galaxy S4. One of the requirements of the application is the presence of SplashScreen, which contains a 1920x1080 image. Its high quality .jpeg image and image size of approximately 2 megabytes .

The problem is that I get an OutOfMemoryError as soon as I launch the application. I'm pretty stunned that this is already happening with an image of just 2 megabytes? How can I fix this problem and display the image?

Resizing or resizing an image is not an option.

SplashScreen.java

public class Splashscreen extends Activity { private static final int SPLASH_DURATION = 2000; private boolean isBackPressed = false; @Override protected void onCreate(Bundle savedInstanceState) { requestWindowFeature(Window.FEATURE_NO_TITLE); getWindow().setFlags(LayoutParams.FLAG_FULLSCREEN, LayoutParams.FLAG_FULLSCREEN); super.onCreate(savedInstanceState); setContentView(R.layout.splashscreen); Handler h = new Handler(); h.postDelayed(new Runnable() { @Override public void run() { // check if the backbutton has been pressed within the splash_duration if(!isBackPressed) { Intent i = new Intent(Splashscreen.this, MainActivity.class); i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); Splashscreen.this.startActivity(i); overridePendingTransition(R.anim.short_fade_in, R.anim.short_fade_out); } finish(); } }, SPLASH_DURATION); } @Override public void onBackPressed() { isBackPressed = true; super.onBackPressed(); } } 

And splashscreen.xml

  <ImageView xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/ivSplashScreenImage" android:layout_width="match_parent" android:layout_height="match_parent" android:scaleType="fitXY" android:src="@drawable/high_res_splashscreen_image"/> 

ADDITIONAL INFORMATION:

Sometimes (when a lot of device memory is available), an application may go past the splash screen, but then the applicationโ€™s memory consumption is just crazy . (about 100 megabytes) . Despite the fact that I close the activity of SplashScreen and exit (), it seems that there is a link to ImageView / Image stored in memory.

How to reduce the amount of memory in memory?

When I do not show the screen saver, my application consumes only about 35 MB of memory. With SplashScreen image - about 100 MB.

+15
android image out-of-memory bitmap android-imageview
source share
2 answers

Three tips to help you:

  • Use this to download images from Download Large Raster Images Documentation for Android :

     public static Bitmap decodeSampledBitmapFromResource(Resources res, int resId, int reqWidth, int reqHeight) { // First decode with inJustDecodeBounds=true to check dimensions final BitmapFactory.Options options = new BitmapFactory.Options(); options.inJustDecodeBounds = true; BitmapFactory.decodeResource(res, resId, options); // Calculate inSampleSize options.inSampleSize = calculateInSampleSize(options, reqWidth, reqHeight); // Decode bitmap with inSampleSize set options.inJustDecodeBounds = false; return BitmapFactory.decodeResource(res, resId, options); } 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) { // Calculate ratios of height and width to requested height and width final int heightRatio = Math.round((float) height / (float) reqHeight); final int widthRatio = Math.round((float) width / (float) reqWidth); // Choose the smallest ratio as inSampleSize value, this will guarantee // a final image with both dimensions larger than or equal to the // requested height and width. inSampleSize = heightRatio < widthRatio ? heightRatio : widthRatio; } return inSampleSize; } 
  • Make sure that you have only one instance of your Bitmap in memory. After displaying it, call recycle() and set the null link. You can use the Tracker Allocation Tracker to find out what is highlighted. You can also read the HPROF files as suggested in the comments.

  • The default pixel format is ARGB_8888 , which means 4 bytes per pixel. Very good article: Raster quality, grouping and smoothing . Your image is JPEG, so it does not have transparency, so you spend 1 byte for each pixel for the alpha channel. This is not very likely, but perhaps with acceptable quality you can use an even more economical format. Look at them. Maybe, for example, RGB_565 . It takes 2 bytes per pixel, so your image will be 50% lighter. You can enable anti-aliasing to improve the quality of RGB_565 .

+13
source share

I had a similar problem and the solution is simple. Place the high resolution image of 1920x1080 pixels in the drawable-nodpi directory . The system does not scale the resources marked with this qualifier , regardless of the current screen density, which leads to OutOfMemoryError, so the problem should disappear.

Please check the Android documentation: http://developer.android.com/intl/es/guide/practices/screens_support.html

Hope this helps.

+5
source share

All Articles