关于png图像的Android内存不足

时间:2012-09-28 08:39:21

标签: android image-processing gimp

我的应用中有一个正确显示的jpg图像。在我的listview中,我想使图像更透明,以便更容易看到文本。我将图像更改为png格式并在GIMP中更改了它的不透明度。

现在新图像位于app drawable文件夹中。我得到以下错误。为什么呢?

   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

感谢。

[更新]

@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);

            }
        });
    }

}

3 个答案:

答案 0 :(得分:2)

活动窗口的默认PixelFormatRGB_565

当您使用JPEG图片时,正在RGB_565 (16位)中阅读。

当您使用透明度(字母数字值)PNG图片时,会在RGBA_8888 (32位)中读取。< / p>

因此,相同的图像尺寸(如尺寸)需要更多内存。

答案 1 :(得分:2)

正如@Benito所说,PNG需要两倍于jpg的内存。因此,您的选择是:

  1. 缩小图像尺寸。

  2. 使用JPG,并使用alpha。不幸的是,你不能在xml中设置alpha,所以你应该以编程方式进行设置(见下文)。

  3. 即使您选择了选项2,我也建议您减小图像尺寸,因为即使它适用于您的手机,其他型号的内存也可能较少。从日志中可以看出你的图像大约是2K * 3K,这可能比所有屏幕都要大。

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

答案 2 :(得分:1)

请尝试以下链接,在这里您将获得一些相关的答案。

Android VM Out of Memory Error

OR

Android png images big in memory

这可能会对你有帮助。