安卓:BitmapFactory.decodeResource 返回 null

标签 android graphics bitmap android-context

我似乎无法弄清楚这一点。我有 2 个具有不同特性的 java 类,每个都调用 BitmapFactory.decodeResource 来获取相同的图像资源,一个返回位图,而另一个返回 null。这两个类都在同一个包中。

这是工作的类,它调用返回位图的 BitmapFactory.decodeResource。我只包含了相关代码。

package advoworks.test;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Rect;
import android.util.Log;
import android.view.Surface;
import android.view.SurfaceHolder;
import android.view.SurfaceView;

public class MainScreen extends SurfaceView implements SurfaceHolder.Callback {

    private static final String TAG = MainScreen.class.getSimpleName();

    public MainScreen(Context context) {
        super(context);

        Bitmap bitmap;
        bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.droid_1);

        //adding the callback (this) to the surface holder to intercept events;
        getHolder().addCallback(this);

        // make the GamePanel focusable so it can handle events
        setFocusable(true);

    }
}

这是不起作用的类。 BitmapFactory.decodeResource 在调试时返回 NULL。我只包含了我认为相关的代码。

package advoworks.test;
import android.content.res.Resources;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.util.Log;

public class Segment {

    private int x;
    private int y;
    private Bitmap bitmap;

    public Segment(int x, int y) {
        Log.d(TAG, "Creating Segment");
        try {
            this.bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.droid_1);
        } catch (Exception e) {
            Log.d(TAG,"Error is " + e);
        }   
        this.x = x;
        this.y = y;
        Log.d(TAG, "Created Segment");
    }
}

有什么线索吗?

最佳答案

检查图像的分辨率,如果太大,BitmapFactory.decodeResource 将返回 null(无一异常(exception))

关于安卓:BitmapFactory.decodeResource 返回 null,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7501863/

相关文章:

java - 安卓蓝牙 : cannot write a Characteristic (no PROPERTY_WRITE)

C# 库节点边图

c# - 如何将 Graphics 转换为 Image 或 Bitmap?

android - 使用 BitmapFactory.decodeResource() 对 PNG 重新采样后出现意外的位图大小

android - KXmlParser 在 RSS 传递开始时抛出 "Unexpected token"异常

java - 在android中反序列化JSON字符串

Android 滑入滑出动画问题

java - 如何在 JPanel 中保持宽高比

c# - 在运行时更改颜色查找纹理

java - 如何在不填充的情况下在位图上绘制圆圈?