【问题标题】:How to reduce the size of the application?如何减小应用程序的大小?
【发布时间】:2012-07-26 03:56:41
【问题描述】:

在我的应用程序中,我使用 54 个图像。为了支持多个屏幕,我创建了这样的文件夹

drawable-hdpi
drawable-mdpi
drawable-xdpi
drawable-mdpi

并粘贴每个文件夹中的所有图像。因此,应用程序大小增加了很多.... 然后我试着喜欢这种方式...... 只取一个文件夹 drawable 保留所有 54 个图像,删除所有 drawabl-mdpi、drawable-hdpi、drawable-mdpi 和 drawable-xdpi 文件夹。 在我的清单文件中,我提到了

"<supports-screens 
android:anyDensity="true" 
android:largeScreens="true" 
android:normalScreens="true" 
android:resizeable="true" 
android:smallScreens="true" />"

但它给了我这样的错误

07-25 15:04:25.928: ERROR/dalvikvm-heap(13963): 2764800-byte external allocation too large for this process.
07-25 15:04:25.928: ERROR/GraphicsJNI(13963): VM won't let us allocate 2764800 bytes
07-25 15:04:25.928: DEBUG/AndroidRuntime(13963): Shutting down VM
07-25 15:04:25.928: WARN/dalvikvm(13963): threadid=1: thread exiting with uncaught exception (group=0x4001d7e0)
07-25 15:04:25.967: ERROR/AndroidRuntime(13963): FATAL EXCEPTION: main
07-25 15:04:25.967: ERROR/AndroidRuntime(13963): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.jacksonville.alenseventapp/com.jacksonville.alenseventapp.HomeActivity}: android.view.InflateException: Binary XML file line #1: Error inflating class <unknown>
07-25 15:04:25.967: ERROR/AndroidRuntime(13963):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2663)
07-25 15:04:25.967: ERROR/AndroidRuntime(13963):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679)
07-25 15:04:25.967: ERROR/AndroidRuntime(13963):     at android.app.ActivityThread.access$2300(ActivityThread.java:125)
07-25 15:04:25.967: ERROR/AndroidRuntime(13963):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033)
07-25 15:04:25.967: ERROR/AndroidRuntime(13963):     at android.os.Handler.dispatchMessage(Handler.java:99)
07-25 15:04:25.967: ERROR/AndroidRuntime(13963):     at android.os.Looper.loop(Looper.java:123)
07-25 15:04:25.967: ERROR/AndroidRuntime(13963):     at android.app.ActivityThread.main(ActivityThread.java:4627)
07-25 15:04:25.967: ERROR/AndroidRuntime(13963):     at java.lang.reflect.Method.invokeNative(Native Method)
07-25 15:04:25.967: ERROR/AndroidRuntime(13963):     at java.lang.reflect.Method.invoke(Method.java:521)
07-25 15:04:25.967: ERROR/AndroidRuntime(13963):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:858)
07-25 15:04:25.967: ERROR/AndroidRuntime(13963):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
07-25 15:04:25.967: ERROR/AndroidRuntime(13963):     at dalvik.system.NativeStart.main(Native Method)
07-25 15:04:25.967: ERROR/AndroidRuntime(13963): Caused by: android.view.InflateException: Binary XML file line #1: Error inflating class <unknown>
07-25 15:04:25.967: ERROR/AndroidRuntime(13963):     at android.view.LayoutInflater.createView(LayoutInflater.java:513)
07-25 15:04:25.967: ERROR/AndroidRuntime(13963):     at com.android.internal.policy.impl.PhoneLayoutInflater.onCreateView(PhoneLayoutInflater.java:56)
07-25 15:04:25.967: ERROR/AndroidRuntime(13963):     at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:563)
07-25 15:04:25.967: ERROR/AndroidRuntime(13963):     at android.view.LayoutInflater.inflate(LayoutInflater.java:385)
07-25 15:04:25.967: ERROR/AndroidRuntime(13963):     at android.view.LayoutInflater.inflate(LayoutInflater.java:320)
07-25 15:04:25.967: ERROR/AndroidRuntime(13963):     at android.view.LayoutInflater.inflate(LayoutInflater.java:276)
07-25 15:04:25.967: ERROR/AndroidRuntime(13963):     at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:198)
07-25 15:04:25.967: ERROR/AndroidRuntime(13963):     at android.app.Activity.setContentView(Activity.java:1647)
07-25 15:04:25.967: ERROR/AndroidRuntime(13963):     at com.jacksonville.alenseventapp.HomeActivity.onCreate(HomeActivity.java:12)
07-25 15:04:25.967: ERROR/AndroidRuntime(13963):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
07-25 15:04:25.967: ERROR/AndroidRuntime(13963):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2627)
07-25 15:04:25.967: ERROR/AndroidRuntime(13963):     ... 11 more
07-25 15:04:25.967: ERROR/AndroidRuntime(13963): Caused by: java.lang.reflect.InvocationTargetException
07-25 15:04:25.967: ERROR/AndroidRuntime(13963):     at android.widget.LinearLayout.<init>(LinearLayout.java:115)
07-25 15:04:25.967: ERROR/AndroidRuntime(13963):     at java.lang.reflect.Constructor.constructNative(Native Method)
07-25 15:04:25.967: ERROR/AndroidRuntime(13963):     at java.lang.reflect.Constructor.newInstance(Constructor.java:446)
07-25 15:04:25.967: ERROR/AndroidRuntime(13963):     at android.view.LayoutInflater.createView(LayoutInflater.java:500)
07-25 15:04:25.967: ERROR/AndroidRuntime(13963):     ... 21 more
07-25 15:04:25.967: ERROR/AndroidRuntime(13963): Caused by: java.lang.OutOfMemoryError: bitmap size exceeds VM budget
07-25 15:04:25.967: ERROR/AndroidRuntime(13963):     at android.graphics.Bitmap.nativeCreate(Native Method)
07-25 15:04:25.967: ERROR/AndroidRuntime(13963):     at android.graphics.Bitmap.createBitmap(Bitmap.java:468)
07-25 15:04:25.967: ERROR/AndroidRuntime(13963):     at android.graphics.Bitmap.createBitmap(Bitmap.java:435)
07-25 15:04:25.967: ERROR/AndroidRuntime(13963):     at android.graphics.Bitmap.createScaledBitmap(Bitmap.java:340)
07-25 15:04:25.967: ERROR/AndroidRuntime(13963):     at android.graphics.BitmapFactory.finishDecode(BitmapFactory.java:488)
07-25 15:04:25.967: ERROR/AndroidRuntime(13963):     at android.graphics.BitmapFactory.decodeStream(BitmapFactory.java:462)
07-25 15:04:25.967: ERROR/AndroidRuntime(13963):     at android.graphics.BitmapFactory.decodeResourceStream(BitmapFactory.java:323)
07-25 15:04:25.967: ERROR/AndroidRuntime(13963):     at android.graphics.drawable.Drawable.createFromResourceStream(Drawable.java:697)
07-25 15:04:25.967: ERROR/AndroidRuntime(13963):     at android.content.res.Resources.loadDrawable(Resources.java:1709)
07-25 15:04:25.967: ERROR/AndroidRuntime(13963):     at android.content.res.TypedArray.getDrawable(TypedArray.java:601)
07-25 15:04:25.967: ERROR/AndroidRuntime(13963):     at android.view.View.<init>(View.java:1885)
07-25 15:04:25.967: ERROR/AndroidRuntime(13963):     at android.view.View.<init>(View.java:1834)
07-25 15:04:25.967: ERROR/AndroidRuntime(13963):     at android.view.ViewGroup.<init>(ViewGroup.java:285)
07-25 15:04:25.967: ERROR/AndroidRuntime(13963):     ... 25 more

如果有人知道这件事请帮助我...

【问题讨论】:

    标签: android android-layout


    【解决方案1】:

    您的应用程序中有四个主要错误

    java.lang.RuntimeException: Unable to start activity 
       **ComponentInfo{com.jacksonville.alenseventapp/com.
       jacksonville.alenseventapp.HomeActivity}:
    

    ** android.view.InflateException: Binary XML file line #1:
           Error inflating class <unknown>
           07-25 15:04:25.967: ERROR/AndroidRuntime(13963):    
           at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2663) 
            and 07-25 15:04:25.967: ERROR/AndroidRuntime(13963): Caused by: 
            android.view.InflateException: Binary XML file line #1: 
            Error inflating class <unknown>
    

    这两个错误表明你指的是一个不存在的包 这可能是由于您遵循执行此代码的教程, 您在应用程序中使用了该代码的包名称

    第三个错误

        07-25 15:04:25.967: ERROR/AndroidRuntime(13963): Caused by: 
    java.lang.reflect.InvocationTargetException
    

    看到这些问题也有同样的问题

    How to remove this error ? java.lang.reflect.InvocationTargetException

    InvocationTargetException on inflating an xml - android

    也看到了这个

    http://developer.android.com/reference/java/lang/reflect/InvocationTargetException.html

    这是内存不足造成的

    Caused by: java.lang.OutOfMemoryError: bitmap size exceeds VM budget
    

    为避免这种情况,您可以使用此方法显示

    // here path is from  where you are picking the image 
    
    
    
    Bitmap myBitmap = ShrinkBitmap(Path, 300, 300);
    
    MyImageView.setImageBitmap(myBitmap );
    
    
    private Bitmap ShrinkBitmap(String file, int width, int height) {
            // TODO Auto-generated method stub
            BitmapFactory.Options bmpFactoryOptions = new BitmapFactory.Options();
            bmpFactoryOptions.inJustDecodeBounds = true;
            Bitmap bitmap = BitmapFactory.decodeFile(file, bmpFactoryOptions);
    
            int heightRatio = (int)Math.ceil(bmpFactoryOptions.outHeight/(float)height);
            int widthRatio = (int)Math.ceil(bmpFactoryOptions.outWidth/(float)width);
    
            if (heightRatio > 1 || widthRatio > 1)
            {
             if (heightRatio > widthRatio)
             {
              bmpFactoryOptions.inSampleSize = heightRatio;
             } else {
              bmpFactoryOptions.inSampleSize = widthRatio;
             }
            }
    
            bmpFactoryOptions.inJustDecodeBounds = false;
            bitmap = BitmapFactory.decodeFile(file, bmpFactoryOptions);
         return bitmap;
        }
    

    【讨论】:

    • drawable-hdpi drawable-mdpi drawable-xdpi drawable-mdpi 如果我遵循这种方法,我不会收到任何错误,但问题是 apk 大小接近 8mb
    • " 问题是 apk 大小接近 8mb" 这是什么问题
    • venkat 应用程序可能需要一些时间来安装,但是一旦安装它的处理就可以了
    【解决方案2】:

    http://developer.android.com/training/displaying-bitmaps/index.html 看起来可能对您有所帮助。

    https://stackoverflow.com/a/8251471/229631 详细介绍了如何在不耗尽内存的情况下加载位图。

    【讨论】:

      【解决方案3】:

      Google I/O 2011: Memory management for Android Apps - 这个视频非常值得花时间观看。您将学到很多关于位图和内存管理的知识。

      【讨论】:

        猜你喜欢
        • 2023-04-04
        • 2021-01-23
        • 1970-01-01
        • 1970-01-01
        • 2021-04-30
        • 2016-02-02
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多