在android中使用gridview的图库

时间:2014-09-08 16:07:56

标签: android gridview gallery

  

您好我使用本教程中的网格视图创建了一个图库   http://www.androidhive.info/2012/02/android-gridview-layout-tutorial/

     

我将70张图片添加到 ImageAdapter.java 并运行app并强行关闭。

09-08 20:24:29.242: D/dalvikvm(4971): GC_EXTERNAL_ALLOC freed <1K, 48% free   2818K/5379K, external 59477K/61525K, paused 26ms
09-08 20:24:29.250: E/dalvikvm-heap(4971): 1638400-byte external allocation too large for this process.
09-08 20:24:29.281: I/dalvikvm-heap(4971): Clamp target GC heap from 65.182MB to 64.000MB
09-08 20:24:29.281: D/dalvikvm(4971): GC_FOR_MALLOC freed 0K, 48% free 2818K/5379K, external 59477K/61525K, paused 20ms
09-08 20:24:29.289: E/GraphicsJNI(4971): VM won't let us allocate 1638400 bytes
09-08 20:24:29.289: D/skia(4971): --- decoder->decode returned false
09-08 20:24:29.312: W/dalvikvm(4971): threadid=1: thread exiting with uncaught exception (group=0x40015578)
09-08 20:24:29.398: E/AndroidRuntime(4971): FATAL EXCEPTION: main
09-08 20:24:29.398: E/AndroidRuntime(4971): java.lang.OutOfMemoryError: bitmap size exceeds VM budget
09-08 20:24:29.398: E/AndroidRuntime(4971):     at android.graphics.BitmapFactory.nativeDecodeAsset(Native Method)
09-08 20:24:29.398: E/AndroidRuntime(4971):     at android.graphics.BitmapFactory.decodeStream(BitmapFactory.java:563)
09-08 20:24:29.398: E/AndroidRuntime(4971):     at android.graphics.BitmapFactory.decodeResourceStream(BitmapFactory.java:439)
09-08 20:24:29.398: E/AndroidRuntime(4971):     at android.graphics.drawable.Drawable.createFromResourceStream(Drawable.java:697)
09-08 20:24:29.398: E/AndroidRuntime(4971):     at android.content.res.Resources.loadDrawable(Resources.java:1709)
09-08 20:24:29.398: E/AndroidRuntime(4971):     at android.content.res.Resources.getDrawable(Resources.java:581)
09-08 20:24:29.398: E/AndroidRuntime(4971):     at android.widget.ImageView.resolveUri(ImageView.java:501)
09-08 20:24:29.398: E/AndroidRuntime(4971):     at android.widget.ImageView.setImageResource(ImageView.java:280)
09-08 20:24:29.398: E/AndroidRuntime(4971):     at com.example.androidhive.ImageAdapter.getView(ImageAdapter.java:110)
09-08 20:24:29.398: E/AndroidRuntime(4971):     at android.widget.AbsListView.obtainView(AbsListView.java:1567)
09-08 20:24:29.398: E/AndroidRuntime(4971):     at android.widget.GridView.makeAndAddView(GridView.java:1254)
09-08 20:24:29.398: E/AndroidRuntime(4971):     at android.widget.GridView.makeRow(GridView.java:300)
09-08 20:24:29.398: E/AndroidRuntime(4971):     at android.widget.GridView.fillDown(GridView.java:253)
09-08 20:24:29.398: E/AndroidRuntime(4971):     at android.widget.GridView.fillGap(GridView.java:220)
09-08 20:24:29.398: E/AndroidRuntime(4971):     at android.widget.AbsListView.trackMotionScroll(AbsListView.java:4132)
09-08 20:24:29.398: E/AndroidRuntime(4971):     at android.widget.AbsListView.onTouchEvent(AbsListView.java:2588)
09-08 20:24:29.398: E/AndroidRuntime(4971):     at android.view.View.dispatchTouchEvent(View.java:3938)
09-08 20:24:29.398: E/AndroidRuntime(4971):     at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:903)
09-08 20:24:29.398: E/AndroidRuntime(4971):     at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:942)
09-08 20:24:29.398: E/AndroidRuntime(4971):     at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:942)
09-08 20:24:29.398: E/AndroidRuntime(4971):     at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:942)
09-08 20:24:29.398: E/AndroidRuntime(4971):     at com.android.internal.policy.impl.PhoneWindow$DecorView.superDispatchTouchEvent(PhoneWindow.java:1733)
09-08 20:24:29.398: E/AndroidRuntime(4971):     at com.android.internal.policy.impl.PhoneWindow.superDispatchTouchEvent(PhoneWindow.java:1151)
09-08 20:24:29.398: E/AndroidRuntime(4971):     at android.app.Activity.dispatchTouchEvent(Activity.java:2096)
09-08 20:24:29.398: E/AndroidRuntime(4971):     at com.android.internal.policy.impl.PhoneWindow$DecorView.dispatchTouchEvent(PhoneWindow.java:1717)
09-08 20:24:29.398: E/AndroidRuntime(4971):     at android.view.ViewRoot.deliverPointerEvent(ViewRoot.java:2215)
09-08 20:24:29.398: E/AndroidRuntime(4971):     at android.view.ViewRoot.handleMessage(ViewRoot.java:1886)
09-08 20:24:29.398: E/AndroidRuntime(4971):     at android.os.Handler.dispatchMessage(Handler.java:99)
09-08 20:24:29.398: E/AndroidRuntime(4971):     at android.os.Looper.loop(Looper.java:130)
09-08 20:24:29.398: E/AndroidRuntime(4971):     at android.app.ActivityThread.main(ActivityThread.java:3689)
09-08 20:24:29.398: E/AndroidRuntime(4971):     at java.lang.reflect.Method.invokeNative(Native Method)
09-08 20:24:29.398: E/AndroidRuntime(4971):     at java.lang.reflect.Method.invoke(Method.java:507)
09-08 20:24:29.398: E/AndroidRuntime(4971):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:842)
09-08 20:24:29.398: E/AndroidRuntime(4971):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:600)
09-08 20:24:29.398: E/AndroidRuntime(4971):     at dalvik.system.NativeStart.main(Native Method)
  

Q1:这个错误是什么?

     

Q2:创建图库图片的最佳方式???

5 个答案:

答案 0 :(得分:1)

你的图像尺寸太大。使用低尺寸图片

答案 1 :(得分:1)

不要一次性加载70张图像,使用lazy loading for loading images,也可以使用低维图像,特别是.png图像。

答案 2 :(得分:1)

Q1:它说:java.lang.OutOfMemoryError: bitmap size exceeds VM budget
这意味着您正在尝试加载太大的图像。它不能保存在VM堆内存中。

Q2:请参阅:http://developer.android.com/training/displaying-bitmaps/load-bitmap.html

答案 3 :(得分:1)

我的ImageGallery库非常易于使用。这可能就是你所需要的。

https://github.com/lawloretienne/ImageGallery

Intent intent = new Intent(MainActivity.this, ImageGalleryActivity.class);

ArrayList<String> images = new ArrayList<>();

images.add("https://images.unsplash.com/photo-1437422061949-f6efbde0a471?q=80&fm=jpg&s=e23055c9ba7686b8fe583fb8318a1f88");
images.add("https://images.unsplash.com/photo-1434139240289-56c519f77cb0?q=80&fm=jpg&s=13f8a0d1c2f96b5f311dedeb17cddb60");
images.add("https://images.unsplash.com/photo-1429152937938-07b5f2828cdd?q=80&fm=jpg&s=a4f424db0ae5a398297df5ae5e0520d6");
images.add("https://images.unsplash.com/photo-1430866880825-336a7d7814eb?q=80&fm=jpg&s=450de8563ac041f48b1563b499f56895");
images.add("https://images.unsplash.com/photo-1429547584745-d8bec594c82e?q=80&fm=jpg&s=e9a7d9973088122a3e453cb2af541201");
images.add("https://images.unsplash.com/photo-1429277158984-614d155e0017?q=80&fm=jpg&s=138f154e17a304b296c953323862633b");
images.add("https://images.unsplash.com/photo-1429042007245-890c9e2603af?q=80&fm=jpg&s=8b76d20174cf46bffe32ea18f05551d3");
images.add("https://images.unsplash.com/photo-1429091967365-492aaa5accfe?q=80&fm=jpg&s=b7430cfe5508430aea39fcf3b0645878");
images.add("https://images.unsplash.com/photo-1430132594682-16e1185b17c5?q=80&fm=jpg&s=a70abbfff85382d11b03b9bbc71649c3");
images.add("https://images.unsplash.com/photo-1436891620584-47fd0e565afb?q=80&fm=jpg&s=33cf5b0ee9fbd292475a0c03bee481c9");

intent.putStringArrayListExtra("images", images);
// optionally set background color using Palette
intent.putExtra("palette_color_type", PaletteColorType.VIBRANT);

startActivity(intent);

答案 4 :(得分:0)

  1. 打开您的 AndroidManifest.xml 文件。
  2. 寻找<application>标签
  3. 将以下属性添加到您的<application>标签

只需复制属性...? 这将解决错误...

<application
      android:hardwareAccelerated="false"
      android:largeHeap="true">
 </application>