我运行这个程序它正常显示但是当我点击这个图像或滚动这个图像时它会崩溃。请帮助我!
我的代码在这里: enter link description here
我的错误:
05-18 14:18:56.064: E/AndroidRuntime(1375): FATAL EXCEPTION: main
05-18 14:18:56.064: E/AndroidRuntime(1375): java.lang.OutOfMemoryError
05-18 14:18:56.064: E/AndroidRuntime(1375): at android.graphics.BitmapFactory.nativeDecodeAsset(Native Method)
05-18 14:18:56.064: E/AndroidRuntime(1375): at android.graphics.BitmapFactory.decodeStream(BitmapFactory.java:502)
05-18 14:18:56.064: E/AndroidRuntime(1375): at android.graphics.BitmapFactory.decodeResourceStream(BitmapFactory.java:355)
05-18 14:18:56.064: E/AndroidRuntime(1375): at android.graphics.drawable.Drawable.createFromResourceStream(Drawable.java:785)
05-18 14:18:56.064: E/AndroidRuntime(1375): at android.content.res.Resources.loadDrawable(Resources.java:1965)
05-18 14:18:56.064: E/AndroidRuntime(1375): at android.content.res.Resources.getDrawable(Resources.java:660)
05-18 14:18:56.064: E/AndroidRuntime(1375): at android.widget.ImageView.resolveUri(ImageView.java:616)
05-18 14:18:56.064: E/AndroidRuntime(1375): at android.widget.ImageView.setImageResource(ImageView.java:349)
05-18 14:18:56.064: E/AndroidRuntime(1375): at com.example.gallerycookbook.GalleryImageAdapter.getView(GalleryImageAdapter.java:47)
05-18 14:18:56.064: E/AndroidRuntime(1375): at android.widget.Gallery.makeAndAddView(Gallery.java:865)
05-18 14:18:56.064: E/AndroidRuntime(1375): at android.widget.Gallery.fillToGalleryRightLtr(Gallery.java:819)
05-18 14:18:56.064: E/AndroidRuntime(1375): at android.widget.Gallery.fillToGalleryRight(Gallery.java:763)
05-18 14:18:56.064: E/AndroidRuntime(1375): at android.widget.Gallery.trackMotionScroll(Gallery.java:393)
05-18 14:18:56.064: E/AndroidRuntime(1375): at android.widget.Gallery.onScroll(Gallery.java:1043)
05-18 14:18:56.064: E/AndroidRuntime(1375): at android.view.GestureDetector.onTouchEvent(GestureDetector.java:572)
05-18 14:18:56.064: E/AndroidRuntime(1375): at android.widget.Gallery.onTouchEvent(Gallery.java:958)
05-18 14:18:56.064: E/AndroidRuntime(1375): at android.view.View.dispatchTouchEvent(View.java:7246)
05-18 14:18:56.064: E/AndroidRuntime(1375): at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2168)
05-18 14:18:56.064: E/AndroidRuntime(1375): at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1903)
05-18 14:18:56.064: E/AndroidRuntime(1375): at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2174)
05-18 14:18:56.064: E/AndroidRuntime(1375): at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1917)
05-18 14:18:56.064: E/AndroidRuntime(1375): at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2174)
05-18 14:18:56.064: E/AndroidRuntime(1375): at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1917)
05-18 14:18:56.064: E/AndroidRuntime(1375): at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2174)
05-18 14:18:56.064: E/AndroidRuntime(1375): at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1917)
05-18 14:18:56.064: E/AndroidRuntime(1375): at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2174)
05-18 14:18:56.064: E/AndroidRuntime(1375): at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1917)
05-18 14:18:56.064: E/AndroidRuntime(1375): at com.android.internal.policy.impl.PhoneWindow$DecorView.superDispatchTouchEvent(PhoneWindow.java:1953)
05-18 14:18:56.064: E/AndroidRuntime(1375): at com.android.internal.policy.impl.PhoneWindow.superDispatchTouchEvent(PhoneWindow.java:1405)
05-18 14:18:56.064: E/AndroidRuntime(1375): at android.app.Activity.dispatchTouchEvent(Activity.java:2410)
05-18 14:18:56.064: E/AndroidRuntime(1375): at com.android.internal.policy.impl.PhoneWindow$DecorView.dispatchTouchEvent(PhoneWindow.java:1901)
05-18 14:18:56.064: E/AndroidRuntime(1375): at android.view.View.dispatchPointerEvent(View.java:7426)
05-18 14:18:56.064: E/AndroidRuntime(1375): at android.view.ViewRootImpl.deliverPointerEvent(ViewRootImpl.java:3220)
05-18 14:18:56.064: E/AndroidRuntime(1375): at android.view.ViewRootImpl.deliverInputEvent(ViewRootImpl.java:3165)
05-18 14:18:56.064: E/AndroidRuntime(1375): at android.view.ViewRootImpl.doProcessInputEvents(ViewRootImpl.java:4292)
05-18 14:18:56.064: E/AndroidRuntime(1375): at android.view.ViewRootImpl.enqueueInputEvent(ViewRootImpl.java:4271)
05-18 14:18:56.064: E/AndroidRuntime(1375): at android.view.ViewRootImpl$WindowInputEventReceiver.onInputEvent(ViewRootImpl.java:4363)
答案 0 :(得分:1)
此行显示问题:
05-18 14:18:56.064: E/AndroidRuntime(1375): FATAL EXCEPTION: main
05-18 14:18:56.064: E/AndroidRuntime(1375): java.lang.OutOfMemoryError
来自oracle doc:
当Java虚拟机由于内存不足而无法分配对象时抛出,垃圾收集器不再提供更多内存。 OutOfMemoryError对象可以由虚拟机构造,就像禁用抑制和/或堆栈跟踪不可写一样。
使用此代码缩小图片尺寸:
private Bitmap downscaleBitmapUsingDensities(final int sampleSize,final int imageResId)
{
final Options bitmapOptions=new Options();
bitmapOptions.inDensity=sampleSize;
bitmapOptions.inTargetDensity=1;
final Bitmap scaledBitmap=BitmapFactory.decodeResource(getResources(),imageResId,bitmapOptions);
scaledBitmap.setDensity(Bitmap.DENSITY_NONE);
return scaledBitmap;
}
答案 1 :(得分:0)
你内存不足。这通常是当您尝试在一个视图中显示许多图像时。您应该有效地使用位图。一种方法是使用像使用延迟加载的Universal Image loader这样的库。有关详细信息,请参阅here。
答案 2 :(得分:0)
您正在尝试打开图像,以填充Android给予进程的整个内存。为避免这种情况,您必须在显示之前关闭样本图像。我建议使用非常酷的库 - Android Query。