简体   繁体   English

Android-用我的背景图片使应用程序崩溃编辑:我的解决方案

[英]Android - With my background image crashes the app EDIT : My solution

the image size is 4 Mb, it's a jpg. 图片大小为4 Mb,是jpg。 I haven't any problem if i try to change the background with another sample image (with a smaller size , for example the ic_launcher sample). 如果我尝试使用另一个示例图像(具有较小的尺寸,例如ic_launcher示例)来更改背景,我没有任何问题。

Should be a problem with a size? 尺寸应该有问题吗? 2667 × 4733 it's the image dimension. 2667×4733是图像尺寸。

I use a samsung s4 mini for debug, it may be useful. 我使用三星s4 mini进行调试,这可能会很有用。

this is the code: 这是代码:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:columnCount="1"
    android:orientation="vertical"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:background="@drawable/mioartista"    
    tools:context="com.example.settingavatar.MyAvatar" >

    <!-- other things -->

</RelativeLayout>

this is the error log 这是错误日志

10-15 10:08:43.461: E/AndroidRuntime(3438): FATAL EXCEPTION: main
10-15 10:08:43.461: E/AndroidRuntime(3438): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.settingavatar/com.example.settingavatar.MyAvatar}: android.view.InflateException: Binary XML file line #2: Error inflating class android.widget.RelativeLayout
10-15 10:08:43.461: E/AndroidRuntime(3438):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2249)
10-15 10:08:43.461: E/AndroidRuntime(3438):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2299)
10-15 10:08:43.461: E/AndroidRuntime(3438):     at android.app.ActivityThread.access$700(ActivityThread.java:154)
10-15 10:08:43.461: E/AndroidRuntime(3438):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1284)
10-15 10:08:43.461: E/AndroidRuntime(3438):     at android.os.Handler.dispatchMessage(Handler.java:99)

EDIT : the solution that i adopted: 编辑:我采用的解决方案:

I changed the dimension. 我改变了尺寸。 i make 5 set of image ( for the drawable folders) for every screen density. 我为每个屏幕密度制作5组图像(用于可绘制文件夹)。 " is not useful use a 4mb's image if the device use at least the 50% of the size". “如果设备至少使用尺寸的50%,则使用4mb的图像无用”。

You said your image has the size of 2667 x 4733 pixels. 您说图像的大小为2667 x 4733像素。 Since this image will be turned into a Bitmap when being displayed, this is too large (in terms of pixels) and will cause your device to run out of memory. 由于此图像在显示时会变成Bitmap ,因此太大 (以像素为单位),将导致设备内存不足。

A 32bit Bitmap of that size will use up 50 MB of RAM. 该大小的32 BitmapBitmap将占用50 MB的RAM。

I recommend to reduce the size of the image drastically and make use of the different resource folders for image sizes . 我建议大幅减小图像的大小,并使用不同的资源文件夹来获取图像大小 drawable-mdpi , etc. drawable-mdpi

Also, you can take a look at this question: High resolution Image - OutOfMemoryError 另外,您可以看一下这个问题: 高分辨率图像-OutOfMemoryError

It deals with handling and loading large size images properly. 它处理正确处理和加载大尺寸图像。

Check name of image. 检查图像名称。 You may not use the name with : uppercase, - , * , and start with number. 您不能将名称与:大写字母,-,*和以数字开头。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM