0

我是安卓新手。我正在做 sd-card 的图像列表表格。以下代码给我错误“614416 字节分配上的内存不足”。

public View getView(int position, View convertView, ViewGroup parent) {
        LayoutInflater layoutInflater = (LayoutInflater) ctx
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        convertView = layoutInflater.inflate(R.layout.ll_sponsor_list_item,
                parent, false);

        final ImageView img = (ImageView) convertView
                .findViewById(R.id.imggrid_item_image);
        String imgurl = ImageName.get(position);

        AsyncImageLoaderv asyncImageLoaderv = new AsyncImageLoaderv();
        Bitmap cachedImage = asyncImageLoaderv.loadDrawable(imgurl,
                new AsyncImageLoaderv.ImageCallback() {
                    public void imageLoaded(Bitmap imageDrawable,
                            String imageUrl) {
                        img.setImageBitmap(imageDrawable);


                    }
                });
        img.setImageBitmap(cachedImage);
        cachedImage.recycle();
        return convertView;
    }

班级:

class AsyncImageLoaderv  {
    int width;
    int height;
    float aspectRatio;
    int newWidth;
    int newHeight;

    public Bitmap loadDrawable(final String imageUrl,
            final ImageCallback imageCallback) {
        final Handler handler = new Handler() {
            @Override
            public void handleMessage(Message message) {
                imageCallback.imageLoaded((Bitmap) message.obj, imageUrl);
            }
        };
        new Thread() {
            @Override
            public void run() {
                try {
                    Log.d("ur", imageUrl);
                    Bitmap drawable = BitmapFactory.decodeFile(imageUrl);
                    width = drawable.getWidth();
                    height = drawable.getWidth();
                    aspectRatio = (float) width / (float) height;
                    newWidth = 98;
                    newHeight = (int) (98 / aspectRatio);
                    Bitmap.createScaledBitmap(drawable, newWidth, newHeight,
                            true);
                    Message message = handler.obtainMessage(0, drawable);
                    handler.sendMessage(message);
                    //this.sleep(1000);

                } catch (Exception e) {
                    Log.e("thread stellent", e.toString());
                }
            }
        }.start();
        return null;
    }

    public interface ImageCallback {
        public void imageLoaded(Bitmap imageBitmap, String imageUrl);
    }
}
4

1 回答 1

0

正如我在那个问题中回答的那样:

Android - decodefile 或 decodeStream 将所选 Uri 转换为位图时出现内存不足错误

位图不能容纳重量级图片。阅读这篇文章,可以减小图像的大小。

于 2013-01-05T11:32:31.523 回答