我是 Android 开发新手,在将图像加载到活动时遇到错误。
以下是我在eclipse中看到的日志:
04-15 19:55:36.316: D/dalvikvm(3317): GC_EXTERNAL_ALLOC freed 41K, 53% free 2551K/5379K, external 1625K/2137K, paused 180ms
04-15 19:55:36.756: D/dalvikvm(3317): GC_EXTERNAL_ALLOC freed 1K, 53% free 2550K/5379K, external 4025K/5027K, paused 42ms
04-15 19:55:37.296: D/dalvikvm(3317): GC_EXTERNAL_ALLOC freed 2K, 53% free 2560K/5379K, external 5570K/5597K, paused 52ms
04-15 19:55:41.306: D/dalvikvm(3317): GC_EXTERNAL_ALLOC freed 10K, 53% free 2579K/5379K, external 6677K/7260K, paused 43ms
04-15 19:55:41.685: D/dalvikvm(3317): GC_EXTERNAL_ALLOC freed 1K, 53% free 2578K/5379K, external 9077K/11125K, paused 51ms
04-15 19:55:41.986: D/dalvikvm(3317): GC_EXTERNAL_ALLOC freed <1K, 53% free 2580K/5379K, external 9377K/11777K, paused 41ms
04-15 19:55:42.695: D/dalvikvm(3317): GC_EXTERNAL_ALLOC freed 1K, 53% free 2578K/5379K, external 13794K/15842K, paused 51ms
04-15 19:55:42.725: E/dalvikvm-heap(3317): 10181472-byte external allocation too large for this process.
04-15 19:55:42.766: E/GraphicsJNI(3317): VM won't let us allocate 10181472 bytes
04-15 19:55:42.766: D/dalvikvm(3317): GC_FOR_MALLOC freed 0K, 53% free 2578K/5379K, external 13794K/15842K, paused 33ms
04-15 19:55:42.775: D/AndroidRuntime(3317): Shutting down VM
04-15 19:55:42.775: W/dalvikvm(3317): threadid=1: thread exiting with uncaught exception (group=0x40015560)
04-15 19:55:42.815: E/AndroidRuntime(3317): FATAL EXCEPTION: main
04-15 19:55:42.815: E/AndroidRuntime(3317): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.digitest.sample/com.digitest.sample.SafetyActivity}: android.view.InflateException: Binary XML file line #16: Error inflating class android.widget.ImageView
04-15 19:55:42.815: E/AndroidRuntime(3317): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1647)
04-15 19:55:42.815: E/AndroidRuntime(3317): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1663)
04-15 19:55:42.815: E/AndroidRuntime(3317): at android.app.ActivityThread.access$1500(ActivityThread.java:117)
04-15 19:55:42.815: E/AndroidRuntime(3317): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:931)
04-15 19:55:42.815: E/AndroidRuntime(3317): at android.os.Handler.dispatchMessage(Handler.java:99)
04-15 19:55:42.815: E/AndroidRuntime(3317): at android.os.Looper.loop(Looper.java:123)
04-15 19:55:42.815: E/AndroidRuntime(3317): at android.app.ActivityThread.main(ActivityThread.java:3683)
04-15 19:55:42.815: E/AndroidRuntime(3317): at java.lang.reflect.Method.invokeNative(Native Method)
04-15 19:55:42.815: E/AndroidRuntime(3317): at java.lang.reflect.Method.invoke(Method.java:507)
04-15 19:55:42.815: E/AndroidRuntime(3317): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
04-15 19:55:42.815: E/AndroidRuntime(3317): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
04-15 19:55:42.815: E/AndroidRuntime(3317): at dalvik.system.NativeStart.main(Native Method)
04-15 19:55:42.815: E/AndroidRuntime(3317): Caused by: android.view.InflateException: Binary XML file line #16: Error inflating class android.widget.ImageView
04-15 19:55:42.815: E/AndroidRuntime(3317): at android.view.LayoutInflater.createView(LayoutInflater.java:518)
04-15 19:55:42.815: E/AndroidRuntime(3317): at com.android.internal.policy.impl.PhoneLayoutInflater.onCreateView(PhoneLayoutInflater.java:56)
04-15 19:55:42.815: E/AndroidRuntime(3317): at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:568)
04-15 19:55:42.815: E/AndroidRuntime(3317): at android.view.LayoutInflater.rInflate(LayoutInflater.java:623)
04-15 19:55:42.815: E/AndroidRuntime(3317): at android.view.LayoutInflater.inflate(LayoutInflater.java:408)
04-15 19:55:42.815: E/AndroidRuntime(3317): at android.view.LayoutInflater.inflate(LayoutInflater.java:320)
04-15 19:55:42.815: E/AndroidRuntime(3317): at android.view.LayoutInflater.inflate(LayoutInflater.java:276)
04-15 19:55:42.815: E/AndroidRuntime(3317): at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:207)
04-15 19:55:42.815: E/AndroidRuntime(3317): at android.app.Activity.setContentView(Activity.java:1657)
04-15 19:55:42.815: E/AndroidRuntime(3317): at com.digitest.sample.SafetyActivity.onCreate(SafetyActivity.java:14)
04-15 19:55:42.815: E/AndroidRuntime(3317): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
04-15 19:55:42.815: E/AndroidRuntime(3317): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1611)
04-15 19:55:42.815: E/AndroidRuntime(3317): ... 11 more
04-15 19:55:42.815: E/AndroidRuntime(3317): Caused by: java.lang.reflect.InvocationTargetException
04-15 19:55:42.815: E/AndroidRuntime(3317): at java.lang.reflect.Constructor.constructNative(Native Method)
04-15 19:55:42.815: E/AndroidRuntime(3317): at java.lang.reflect.Constructor.newInstance(Constructor.java:415)
04-15 19:55:42.815: E/AndroidRuntime(3317): at android.view.LayoutInflater.createView(LayoutInflater.java:505)
04-15 19:55:42.815: E/AndroidRuntime(3317): ... 22 more
04-15 19:55:42.815: E/AndroidRuntime(3317): Caused by: java.lang.OutOfMemoryError: bitmap size exceeds VM budget
04-15 19:55:42.815: E/AndroidRuntime(3317): at android.graphics.Bitmap.nativeCreate(Native Method)
04-15 19:55:42.815: E/AndroidRuntime(3317): at android.graphics.Bitmap.createBitmap(Bitmap.java:477)
04-15 19:55:42.815: E/AndroidRuntime(3317): at android.graphics.Bitmap.createBitmap(Bitmap.java:444)
04-15 19:55:42.815: E/AndroidRuntime(3317): at android.graphics.Bitmap.createScaledBitmap(Bitmap.java:349)
04-15 19:55:42.815: E/AndroidRuntime(3317): at android.graphics.BitmapFactory.finishDecode(BitmapFactory.java:498)
04-15 19:55:42.815: E/AndroidRuntime(3317): at android.graphics.BitmapFactory.decodeStream(BitmapFactory.java:473)
04-15 19:55:42.815: E/AndroidRuntime(3317): at android.graphics.BitmapFactory.decodeResourceStream(BitmapFactory.java:336)
04-15 19:55:42.815: E/AndroidRuntime(3317): at android.graphics.drawable.Drawable.createFromResourceStream(Drawable.java:697)
04-15 19:55:42.815: E/AndroidRuntime(3317): at android.content.res.Resources.loadDrawable(Resources.java:1709)
04-15 19:55:42.815: E/AndroidRuntime(3317): at android.content.res.TypedArray.getDrawable(TypedArray.java:601)
04-15 19:55:42.815: E/AndroidRuntime(3317): at android.widget.ImageView.<init>(ImageView.java:118)
04-15 19:55:42.815: E/AndroidRuntime(3317): at android.widget.ImageView.<init>(ImageView.java:108)
04-15 19:55:42.815: E/AndroidRuntime(3317): ... 25 more
我的图像是 png 图像,大小仅为 130KB,尽管尺寸为 741x1526px。但我无法想象为什么模拟器无法处理这样的图像大小。我加载了这张图片的较小版本,大小约为 120KB,尺寸为 370 × 762px,加载得很好。
这是我实现 imageview 活动的方式:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
android:background="@drawable/safety_base" >
<ImageView
android:id="@+id/imgSafety"
android:contentDescription="@string/imgSafety_desc"
android:layout_width="1000dp"
android:layout_height="wrap_content"
android:maxHeight="180dp"
android:layout_centerHorizontal="true"
android:layout_alignParentBottom="true"
android:background="@android:color/transparent"
android:src="@drawable/safety_info_large" />
</RelativeLayout>
知道为什么吗?
提前致谢。