【问题标题】:image gallery with gridview in androidandroid中带有gridview的图片库
【发布时间】:2014-11-01 22:07:41
【问题描述】:

嗨,我从本教程创建了一个带有网格视图的图片库 http://www.androidhive.info/2012/02/android-gridview-layout-tutorial/

我将 70 个图像添加到 ImageAdapter.java 并运行应用程序并强制关闭。

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:创建图库图片的最佳方式是什么???

【问题讨论】:

  • Q1:上面写着:java.lang.OutOfMemoryError: bitmap size exceeds VM budget
  • 这是什么?我的记忆有问题还是我的应用程序有问题?
  • 您正在尝试加载太大的图像。它不能保存在 VM 堆内存中。

标签: android gridview gallery


【解决方案1】:

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

Q2:见:http://developer.android.com/training/displaying-bitmaps/load-bitmap.html

【讨论】:

    【解决方案2】:

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

    【讨论】:

      【解决方案3】:

      不要一次加载70张图片,使用lazy loading for loading images,也可以使用低维图片,尤其是.png图片。

      【讨论】:

        【解决方案4】:

        我的 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);
        

        【讨论】:

          【解决方案5】:
          1. 打开您的 AndroidManifest.xml 文件。
          2. 寻找&lt;application&gt;标签
          3. 将以下属性添加到您的 &lt;application&gt; 标记中

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

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

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2021-12-29
            • 1970-01-01
            • 1970-01-01
            相关资源
            最近更新 更多