Android ImageView内存管理

时间:2014-01-01 10:50:52

标签: android memory imageview

我有一个mainactivity类,它设置一个内容视图,上面有几个ImageViews。 在单击其中一个ImageView时,将创建一个新的Intent并开始一个新的活动。

问题是当加载具有ImageView的新内容视图时,应用程序内存不足。 有没有办法删除不再从内存中显示的旧内容视图或ImageView?

这是我的logcat:

01-01 11:08:19.255: D/dalvikvm(416): GC_EXTERNAL_ALLOC freed 15K, 52% free 2587K/5379K, external 9858K/10106K, paused 23ms
01-01 11:08:19.265: E/dalvikvm-heap(416): 1895348-byte external allocation too large for this process.
01-01 11:08:19.295: I/dalvikvm-heap(416): Clamp target GC heap from 16.703MB to 16.000MB
01-01 11:08:19.295: E/GraphicsJNI(416): VM won't let us allocate 1895348 bytes
01-01 11:08:19.295: D/dalvikvm(416): GC_FOR_MALLOC freed <1K, 52% free 2586K/5379K, external 9858K/10106K, paused 17ms
01-01 11:08:19.305: D/AndroidRuntime(416): Shutting down VM
01-01 11:08:19.305: W/dalvikvm(416): threadid=1: thread exiting with uncaught exception (group=0x40015560)
01-01 11:08:19.315: E/AndroidRuntime(416): FATAL EXCEPTION: main
01-01 11:08:19.315: E/AndroidRuntime(416): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.turbo/com.example.turbo.ByReferenceBookList}: android.view.InflateException: Binary XML file line #7: Error inflating class <unknown>
01-01 11:08:19.315: E/AndroidRuntime(416):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1647)
01-01 11:08:19.315: E/AndroidRuntime(416):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1663)
01-01 11:08:19.315: E/AndroidRuntime(416):  at android.app.ActivityThread.access$1500(ActivityThread.java:117)
01-01 11:08:19.315: E/AndroidRuntime(416):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:931)
01-01 11:08:19.315: E/AndroidRuntime(416):  at android.os.Handler.dispatchMessage(Handler.java:99)
01-01 11:08:19.315: E/AndroidRuntime(416):  at android.os.Looper.loop(Looper.java:123)
01-01 11:08:19.315: E/AndroidRuntime(416):  at android.app.ActivityThread.main(ActivityThread.java:3683)
01-01 11:08:19.315: E/AndroidRuntime(416):  at java.lang.reflect.Method.invokeNative(Native Method)
01-01 11:08:19.315: E/AndroidRuntime(416):  at java.lang.reflect.Method.invoke(Method.java:507)
01-01 11:08:19.315: E/AndroidRuntime(416):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
01-01 11:08:19.315: E/AndroidRuntime(416):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
01-01 11:08:19.315: E/AndroidRuntime(416):  at dalvik.system.NativeStart.main(Native Method)
01-01 11:08:19.315: E/AndroidRuntime(416): Caused by: android.view.InflateException: Binary XML file line #7: Error inflating class <unknown>
01-01 11:08:19.315: E/AndroidRuntime(416):  at android.view.LayoutInflater.createView(LayoutInflater.java:518)
01-01 11:08:19.315: E/AndroidRuntime(416):  at com.android.internal.policy.impl.PhoneLayoutInflater.onCreateView(PhoneLayoutInflater.java:56)
01-01 11:08:19.315: E/AndroidRuntime(416):  at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:568)
01-01 11:08:19.315: E/AndroidRuntime(416):  at android.view.LayoutInflater.rInflate(LayoutInflater.java:623)
01-01 11:08:19.315: E/AndroidRuntime(416):  at android.view.LayoutInflater.inflate(LayoutInflater.java:408)
01-01 11:08:19.315: E/AndroidRuntime(416):  at android.view.LayoutInflater.inflate(LayoutInflater.java:320)
01-01 11:08:19.315: E/AndroidRuntime(416):  at android.view.LayoutInflater.inflate(LayoutInflater.java:276)
01-01 11:08:19.315: E/AndroidRuntime(416):  at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:207)
01-01 11:08:19.315: E/AndroidRuntime(416):  at android.app.Activity.setContentView(Activity.java:1657)
01-01 11:08:19.315: E/AndroidRuntime(416):  at com.example.turbo.ByReferenceBookList.onCreate(ByReferenceBookList.java:31)
01-01 11:08:19.315: E/AndroidRuntime(416):  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
01-01 11:08:19.315: E/AndroidRuntime(416):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1611)
01-01 11:08:19.315: E/AndroidRuntime(416):  ... 11 more
01-01 11:08:19.315: E/AndroidRuntime(416): Caused by: java.lang.reflect.InvocationTargetException
01-01 11:08:19.315: E/AndroidRuntime(416):  at java.lang.reflect.Constructor.constructNative(Native Method)
01-01 11:08:19.315: E/AndroidRuntime(416):  at java.lang.reflect.Constructor.newInstance(Constructor.java:415)
01-01 11:08:19.315: E/AndroidRuntime(416):  at android.view.LayoutInflater.createView(LayoutInflater.java:505)
01-01 11:08:19.315: E/AndroidRuntime(416):  ... 22 more
01-01 11:08:19.315: E/AndroidRuntime(416): Caused by: java.lang.OutOfMemoryError: bitmap size exceeds VM budget
01-01 11:08:19.315: E/AndroidRuntime(416):  at android.graphics.Bitmap.nativeCreate(Native Method)
01-01 11:08:19.315: E/AndroidRuntime(416):  at android.graphics.Bitmap.createBitmap(Bitmap.java:477)
01-01 11:08:19.315: E/AndroidRuntime(416):  at android.graphics.Bitmap.createBitmap(Bitmap.java:444)
01-01 11:08:19.315: E/AndroidRuntime(416):  at android.graphics.Bitmap.createScaledBitmap(Bitmap.java:349)
01-01 11:08:19.315: E/AndroidRuntime(416):  at android.graphics.BitmapFactory.finishDecode(BitmapFactory.java:498)
01-01 11:08:19.315: E/AndroidRuntime(416):  at android.graphics.BitmapFactory.decodeStream(BitmapFactory.java:473)
01-01 11:08:19.315: E/AndroidRuntime(416):  at android.graphics.BitmapFactory.decodeResourceStream(BitmapFactory.java:336)
01-01 11:08:19.315: E/AndroidRuntime(416):  at android.graphics.drawable.Drawable.createFromResourceStream(Drawable.java:697)
01-01 11:08:19.315: E/AndroidRuntime(416):  at android.content.res.Resources.loadDrawable(Resources.java:1709)
01-01 11:08:19.315: E/AndroidRuntime(416):  at android.content.res.TypedArray.getDrawable(TypedArray.java:601)
01-01 11:08:19.315: E/AndroidRuntime(416):  at android.widget.ImageView.<init>(ImageView.java:118)
01-01 11:08:19.315: E/AndroidRuntime(416):  at android.widget.ImageView.<init>(ImageView.java:108)
01-01 11:08:19.315: E/AndroidRuntime(416):  ... 25 more

我使用以下代码开始新活动:

Intent byreferencelist = new Intent(getBaseContext(), ByReferenceList.class);
                    startActivity(byreferencelist);
                    finish();

然后我有:

public class ByReferenceList extends Activity {

  protected void onCreate(Bundle savedInstanceState) {
       super.onCreate(savedInstanceState);

       setContentView(R.layout.byreferencelist);

显示: http://pastebin.com/ADuE6C4N (抱歉由于某种原因无法发布XML。)

3 个答案:

答案 0 :(得分:2)

当您开始新活动时,请使用此

Intent intent = new Intent(getApplicationContext(),yourActivity.class);
startActivity(intent);
finish();

答案 1 :(得分:0)

我遇到了同样的问题,但情况有点不同。尝试使用BitmapFactory.Options课程。并将InSampleSize的属性设置为2的幂。如果会减小图像的大小。

创建图像/位图时,请将Options作为参数传递。

BitmapFactory.Options options = new BitmapFactory.Options();
options.InSampleSize = 2; // It can be 2, 4 ,8 ....    
Bitmap bm = BitmapFactory.Create(Bitmap bm, options);

我不确定代码。只需阅读Android文档。

谢谢!

答案 2 :(得分:0)

您应该阅读以下两个Android官方教程。他们将教你如何有效地加载大位图,并提供我们在生产应用程序中使用的工作代码示例

Displays Bitmaps Efficiently

Loading Large Bitmaps Efficiently