android - 将多个图像加载到gridview android

标签 android image gridview bitmap

我有大约 300 张 jpg 图像,我想将这些图像加载到 GridView 中。我正在关注 tutorial

但是当我尝试加载图像时(如果它小于 25 就可以了)然后我得到了内存不足的错误。

private ArrayList getData() {
        final ArrayList imageItems = new ArrayList();
        // retrieve String drawable array
        TypedArray imgs = getResources().obtainTypedArray(R.array.image_ids);
        for (int i = 0; i < imgs.length(); i++) {
            Bitmap bitmap = BitmapFactory.decodeResource(this.getResources(),
                    imgs.getResourceId(i, -1));
            imageItems.add(new ImageItem(bitmap, "Image#" + i));
        }

        return imageItems;

    }

这是加载图片的代码

customGridAdapter = new GridViewAdapter(this, R.layout.row_grid, getData());
        gridView.setAdapter(customGridAdapter);

这是设置图像的代码。你们知道如何将我的所有(200 多张图像)加载到我的 GridView 吗?

谢谢

最佳答案

要解决 android.graphics.BitmapFactory.nativeDecodeByteArray 的 java.lang.OutOfMemoryError 异常,您应该使用以下代码:

BitmapFactory.Options options=new BitmapFactory.Options();// Create object of bitmapfactory's option method for further option use
                options.inPurgeable = true; // inPurgeable is used to free up memory while required
                Bitmap songImage1 = BitmapFactory.decodeByteArray(thumbnail,0, thumbnail.length,options);//Decode image, "thumbnail" is the object of image file
                Bitmap songImage = Bitmap.createScaledBitmap(songImage1, 50 , 50 , true);// convert decoded bitmap into well scalled Bitmap format.

imageview.SetImageDrawable(songImage);

关于android - 将多个图像加载到gridview android,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25801115/

相关文章:

android - 如何获取列表中应用程序的图标?

android - SurfaceView.setbackground() 控制着我的 Canvas

android - 如何使 Edittext 不可点击

python - 如何使用 openCV-python 识别白色背景上的多张照片?

android - Android 中的 GridView 行背景图片

android - picasso 图书馆和 GridView 图像

android - Cordova CLI - 不更新 config.xml

image - 适用于 OSX 的最佳程序员图像编辑器

wpf - WPF用户控件: Image disappears even with 'x:Shared=“False” '

c# - 绑定(bind)到数据表时如何设置gridview列宽