Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

OutOfMemoryError: Using a image which is 1080*1920 do the splash page [duplicate]

I want to implement a splash page in my android app. So in the layout xml, there is a imageView and the image file is a png file which is 1080*1920, when a run the app, it occur a OutOfMemoryError error, how can I fix that?

The XML file:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="match_parent"
    android:layout_height="match_parent">

    <ImageView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="@drawable/welcome1"/>

</LinearLayout>

The error info:

04-17 18:40:16.038  29793-29793/cn.test.android E/AndroidRuntime﹕ FATAL EXCEPTION: main
Process: cn.test.android, PID: 29793
java.lang.OutOfMemoryError
        at android.graphics.BitmapFactory.nativeDecodeAsset(Native Method)
        at android.graphics.BitmapFactory.decodeStream(BitmapFactory.java:597)
        at android.graphics.BitmapFactory.decodeResourceStream(BitmapFactory.java:432)
        at android.graphics.drawable.Drawable.createFromResourceStream(Drawable.java:840)
        at android.content.res.Resources.createFromResourceStream(Resources.java:2477)
        at android.content.res.Resources.loadDrawable(Resources.java:2136)
        at android.content.res.MiuiResources.loadDrawable(MiuiResources.java:320)
        at android.content.res.Resources.getDrawable(Resources.java:710)
        at android.widget.ImageView.resolveUri(ImageView.java:638)
        at android.widget.ImageView.setImageResource(ImageView.java:367)
        at cn.test.android.LandingPageActivity$1.instantiateItem(LandingPageActivity.java:40)
        at android.support.v4.view.ViewPager.addNewItem(ViewPager.java:837)
        at android.support.v4.view.ViewPager.populate(ViewPager.java:987)
        at android.support.v4.view.ViewPager.populate(ViewPager.java:919)
        at android.support.v4.view.ViewPager.onMeasure(ViewPager.java:1441)
        at android.view.View.measure(View.java:16508)
        at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5125)
        at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1404)
        at android.widget.LinearLayout.measureVertical(LinearLayout.java:695)
        at android.widget.LinearLayout.onMeasure(LinearLayout.java:588)
        at android.view.View.measure(View.java:16508)
        at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5125)
        at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
        at android.view.View.measure(View.java:16508)
        at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5125)
        at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1404)
        at android.widget.LinearLayout.measureVertical(LinearLayout.java:695)
        at android.widget.LinearLayout.onMeasure(LinearLayout.java:588)
        at android.view.View.measure(View.java:16508)
        at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5125)
        at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
        at com.android.internal.policy.impl.PhoneWindow$DecorView.onMeasure(PhoneWindow.java:2291)
        at android.view.View.measure(View.java:16508)
        at android.view.ViewRootImpl.performMeasure(ViewRootImpl.java:1946)
        at android.view.ViewRootImpl.measureHierarchy(ViewRootImpl.java:1143)
        at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1325)
        at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1030)
        at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:5639)
        at android.view.Choreographer$CallbackRecord.run(Choreographer.java:761)
        at android.view.Choreographer.doCallbacks(Choreographer.java:574)
        at android.view.Choreographer.doFrame(Choreographer.java:544)
        at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:747)
        at android.os.Handler.handleCallback(Handler.java:733)
        at android.os.Handler.dispatchMessage(Handler.java:95)
        at android.os.Looper.loop(Looper.java:136)
        at android.app.ActivityThread.main(ActivityThread.java:5047)
        at java.lang.reflect.Method.invokeNative(Native Method)
        at java.lang.reflect.Method.invoke(Method.java:515)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:806)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:622)
        at dalvik.system.NativeStart.main(Native Method)
like image 421
user2655973 Avatar asked Apr 17 '15 10:04

user2655973


3 Answers

If you have welcome1.png in the drawable folder, put it in the drawable-nodpi.

This is because drawable is interpreted as drawable-mdpi, so when you load it on a higher density phone it will be upscaled (becoming a gigantic bitmap).

like image 170
David Corsalini Avatar answered Oct 18 '22 21:10

David Corsalini


memory allocated to per App is sparse in android you should not be giving it to images just like that.

your application is running low on memory due to which you are getting this error.

what you can do is increase cahce memory of emulator to support your memory usage. 

well that's not the exact solution, try minimizing image resolution and you can get rid of this error,

like image 2
Pankaj Nimgade Avatar answered Oct 18 '22 20:10

Pankaj Nimgade


Android in general has problems with images, especially large ones. This is due to the maximum amount an App is allowed to allocate. You can resample bitmaps as you load them in.

As a short example, this will reduce the size of the image by half:

final BitmapFactory.Options opts = new BitmapFactory.Options();
opts.inSampleSize = 2;
Bitmap bm = BitmapFactory.decodeStream(is, null, opts);
like image 2
Knossos Avatar answered Oct 18 '22 20:10

Knossos