Android Out of memory regarding png image

我只是一个虾纸丫 提交于 2019-12-11 07:49:31

问题


I have a jpg image in my app that shows correctly. In my listview i'd like to make the image more transparent so it is easier to see the text. I changed the image to a png format and altered it's opacity in GIMP.

Now that the new image is in the app drawable folder. Im getting the following error. why?

   09-28 09:24:07.560: I/global(20140): call socket shutdown, tmpsocket=Socket[address=/178.250.50.40,port=80,localPort=35172]
09-28 09:24:07.570: I/global(20140): call socket shutdown, tmpsocket=Socket[address=/212.169.27.217,port=84,localPort=55656]
09-28 09:24:07.690: D/dalvikvm(20140): GC_FOR_ALLOC freed 113K, 4% free 38592K/39907K, paused 32ms
09-28 09:24:07.690: I/dalvikvm-heap(20140): Forcing collection of SoftReferences for 28072816-byte allocation
09-28 09:24:07.740: D/dalvikvm(20140): GC_BEFORE_OOM freed 9K, 4% free 38582K/39907K, paused 43ms
09-28 09:24:07.740: E/dalvikvm-heap(20140): Out of memory on a 28072816-byte allocation.
09-28 09:24:07.740: I/dalvikvm(20140): "main" prio=5 tid=1 RUNNABLE
09-28 09:24:07.740: I/dalvikvm(20140):   | group="main" sCount=0 dsCount=0 obj=0x40a57490 self=0x1b6e9a8
09-28 09:24:07.740: I/dalvikvm(20140):   | sysTid=20140 nice=0 sched=0/0 cgrp=default handle=1074361640
09-28 09:24:07.740: I/dalvikvm(20140):   | schedstat=( 2289118000 760844000 2121 ) utm=195 stm=33 core=1
09-28 09:24:07.740: I/dalvikvm(20140):   at android.graphics.BitmapFactory.nativeDecodeAsset(Native Method)
09-28 09:24:07.740: I/dalvikvm(20140):   at android.graphics.BitmapFactory.decodeResourceStream(BitmapFactory.java:486)
09-28 09:24:07.740: I/dalvikvm(20140):   at android.graphics.drawable.Drawable.createFromResourceStream(Drawable.java:773)
09-28 09:24:07.740: I/dalvikvm(20140):   at android.content.res.Resources.loadDrawable(Resources.java:2042)
09-28 09:24:07.740: I/dalvikvm(20140):   at android.content.res.TypedArray.getDrawable(TypedArray.java:601)
09-28 09:24:07.740: I/dalvikvm(20140):   at android.view.View.<init>(View.java:2812)
09-28 09:24:07.740: I/dalvikvm(20140):   at android.view.ViewGroup.<init>(ViewGroup.java:410)
09-28 09:24:07.740: I/dalvikvm(20140):   at android.widget.LinearLayout.<init>(LinearLayout.java:174)
09-28 09:24:07.740: I/dalvikvm(20140):   at android.widget.LinearLayout.<init>(LinearLayout.java:170)
09-28 09:24:07.740: I/dalvikvm(20140):   at java.lang.reflect.Constructor.constructNative(Native Method)
09-28 09:24:07.740: I/dalvikvm(20140):   at java.lang.reflect.Constructor.newInstance(Constructor.java:417)
09-28 09:24:07.740: I/dalvikvm(20140):   at android.view.LayoutInflater.createView(LayoutInflater.java:586)
09-28 09:24:07.740: I/dalvikvm(20140):   at com.android.internal.policy.impl.PhoneLayoutInflater.onCreateView(PhoneLayoutInflater.java:56)
09-28 09:24:07.740: I/dalvikvm(20140):   at android.view.LayoutInflater.onCreateView(LayoutInflater.java:653)
09-28 09:24:07.740: I/dalvikvm(20140):   at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:678)
09-28 09:24:07.740: I/dalvikvm(20140):   at android.view.LayoutInflater.inflate(LayoutInflater.java:466)
09-28 09:24:07.740: I/dalvikvm(20140):   at android.view.LayoutInflater.inflate(LayoutInflater.java:396)
09-28 09:24:07.740: I/dalvikvm(20140):   at android.view.LayoutInflater.inflate(LayoutInflater.java:352)
09-28 09:24:07.740: I/dalvikvm(20140):   at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:278)
09-28 09:24:07.740: I/dalvikvm(20140):   at android.app.Activity.setContentView(Activity.java:1897)
09-28 09:24:07.740: I/dalvikvm(20140):   at com.carefreegroup.ShowMoreDetails.onCreate(ShowMoreDetails.java:26)
09-28 09:24:07.740: I/dalvikvm(20140):   at android.app.Activity.performCreate(Activity.java:4543)
09-28 09:24:07.740: I/dalvikvm(20140):   at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1071)
09-28 09:24:07.740: I/dalvikvm(20140):   at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2181)
09-28 09:24:07.740: I/dalvikvm(20140):   at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2260)
09-28 09:24:07.740: I/dalvikvm(20140):   at android.app.ActivityThread.access$600(ActivityThread.java:139)
09-28 09:24:07.740: I/dalvikvm(20140):   at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1277)
09-28 09:24:07.740: I/dalvikvm(20140):   at android.os.Handler.dispatchMessage(Handler.java:99)
09-28 09:24:07.740: I/dalvikvm(20140):   at android.os.Looper.loop(Looper.java:156)
09-28 09:24:07.740: I/dalvikvm(20140):   at android.app.ActivityThread.main(ActivityThread.java:5045)
09-28 09:24:07.740: I/dalvikvm(20140):   at java.lang.reflect.Method.invokeNative(Native Method)
09-28 09:24:07.740: I/dalvikvm(20140):   at java.lang.reflect.Method.invoke(Method.java:511)
09-28 09:24:07.740: I/dalvikvm(20140):   at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
09-28 09:24:07.740: I/dalvikvm(20140):   at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
09-28 09:24:07.740: I/dalvikvm(20140):   at dalvik.system.NativeStart.main(Native Method)
09-28 09:24:07.740: E/dalvikvm(20140): Out of memory: Heap Size=46115KB, Allocated=38582KB, Limit=65536KB
09-28 09:24:07.740: E/dalvikvm(20140): Extra info: Footprint=39907KB, Allowed Footprint=46115KB, Trimmed=892KB
09-28 09:24:07.740: E/Bitmap_JNI(20140): Create Bitmap Failed.
09-28 09:24:07.740: A/libc(20140): Fatal signal 11 (SIGSEGV) at 0x00000004 (code=1)
09-28 09:24:09.750: I/dalvikvm(20367): Turning on JNI app bug workarounds for target SDK version 10...
09-28 09:24:09.940: D/dalvikvm(20367): GC_CONCURRENT freed 864K, 21% free 3797K/4771K, paused 2ms+2ms

thanks.

[update]

@Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.showmoredetailslayout);
        actualCallTime = (TextView)findViewById(R.id.actualcalltime);
        doubleUp = (TextView)findViewById(R.id.doubleupcallid);
        needName = (TextView)findViewById(R.id.needname);
        needNameLabel = (TextView)findViewById(R.id.neednamelabel);
        getRotaDetails = (Button)findViewById(R.id.buttongetrotadetails);
        intent = this.getIntent();
        String actualTimeIn = intent.getStringExtra("actTimeIn");
        String actualTimeOut = intent.getStringExtra("actTimeOut");
        String doubleUpValue = intent.getStringExtra("doubleUpValue");
        String needNameWithCommas = intent.getStringExtra("needNameWithCommas");
        callID = intent.getStringExtra("callID");
        String[] needs = needNameWithCommas.split(",");



        actualCallTime.setText("This call was completed at " + actualTimeIn + " -" + actualTimeOut);
        if( ! doubleUpValue.equalsIgnoreCase("") || doubleUpValue.equalsIgnoreCase("]")){
        doubleUp.setText("This call was not a double up ");
        }else{

            doubleUp.setText("This call was  a double up " + doubleUpValue);
        }

        needNameLabel.setText("Purpose of Call: ");

        for (int i = 0; i < needs.length; i++){
        needName.append( needs[i] + "\n");
        }

        getRotaDetails.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {

                Intent intent = new Intent(ShowMoreDetails.this, GetRotaDetails.class);
                intent.putExtra("callIDExtra", callID);
                startActivity(intent);

            }
        });
    }

}

回答1:


As @Benito said, PNG needs twice the memory of jpg. Therefore Your options are:

  1. Reduce the size of the image.

  2. Use JPG, and use an alpha for it. Unfortunately, you can not set alpha in xml, so you should do it programatically (see below).

I would suggest to decrease image size even if you choose option 2, as even if it works on your phone, other models may have less memory. From the log it seems your image is about 2K*3K, which is larger that probably all screens out there.

View view = findViewById(R.id.background);
Drawable background = view.getBackground();
background.setAlpha(80);



回答2:


The default PixelFormat of your activity's window is RGB_565.

When you were using a JPEG image, it was being read in RGB_565 (16-bit).

When you used a PNG image with transparency (alpha values), it was read in RGBA_8888 (32-bit).

Therefore the same image size (as in dimensions) needed more memory.




回答3:


Try the bellow links, here you will have some related answers.

Android VM Out of Memory Error

OR

Android png images big in memory

This may help you.



来源:https://stackoverflow.com/questions/12636510/android-out-of-memory-regarding-png-image

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!