decodeResource在添加选项时返回null

时间:2013-12-19 22:53:01

标签: android

您好我正在使用以下方法将Bitmap设置为ImageView:

        o = new BitmapFactory.Options();
        o.inJustDecodeBounds = true;
        o.inDither = false;
        o.inPurgeable = true;
        o.inInputShareable = true;

        b = BitmapFactory.decodeResource(getApplicationContext()
                .getResources(), resourceId);
                     //Note the Options parameter is ignored
        ivWallpaper = (ImageView) findViewById(R.id.ivWallpaper);
        ivWallpaper.setImageBitmap(b);
        ivWallpaper.setOnClickListener(Wallpapers.this);

哪个工作正常,但是当我添加选项参数时:

        o = new BitmapFactory.Options();
        o.inJustDecodeBounds = true;
        o.inDither = false;
        o.inPurgeable = true;
        o.inInputShareable = true;

        b = BitmapFactory.decodeResource(getApplicationContext()
                .getResources(), resourceId, o);
        ivWallpaper = (ImageView) findViewById(R.id.ivWallpaper);
        ivWallpaper.setImageBitmap(b);
        ivWallpaper.setOnClickListener(Wallpapers.this);

它给我一个nullPointerException:             ivWallpaper.setImageBitmap(B);

请帮帮我。一直试图解决这个问题

1 个答案:

答案 0 :(得分:4)

http://developer.android.com/reference/android/graphics/BitmapFactory.Options.html

public boolean inJustDecodeBounds

如果设置为true,解码器将返回null(无位图),但仍将设置out ...字段,允许调用者查询位图而无需为其像素分配内存。