Android:BitmapFactory.decodeResource 返回 null

发布于 2024-12-05 21:27:17 字数 1813 浏览 0 评论 0原文

我似乎无法弄清楚这一点。我有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");
    }
}

有人有任何线索吗?

I can't seem to figure this out. I have 2 java classes with different characteristics, each calling BitmapFactory.decodeResource to get the same image resource, one returns the bitmap while the other returns null. Both classes are in the same package.

Here is the class that works, it calls BitmapFactory.decodeResource which returns the bitmap. I've only included relevant code.

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

    }
}

Here is the class that doesn't work. BitmapFactory.decodeResource returns a NULL in debug. I've only included code i felt was relevant.

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");
    }
}

Any clue anyone?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

柳絮泡泡 2024-12-12 21:27:17

检查图像的分辨率,如果太大,BitmapFactory.decodeResource 将返回 null (也不例外)

Check the resolution of your image, if its too big, the BitmapFactory.decodeResource will just return null (no exception)

一曲琵琶半遮面シ 2024-12-12 21:27:17

getResources() 是一个 Context 类方法,并且您没有在 Segment 类中使用上下文。它是如何运作的。您应该调用 getApplicationContext().getResources()

您应该将上下文传递给 Segment 构造函数。

public Segment(Context context, int x, int y) {
    ....
    bitmap = BitmapFactory.decodeResource(context.getResources(), R.drawable.droid_1);
    ....
}

The getResources() is a Context class method and you are not using a context in your Segment class. How does it work. You should call getApplicationContext().getResources()

You should pass the context to the Segment constructor.

public Segment(Context context, int x, int y) {
    ....
    bitmap = BitmapFactory.decodeResource(context.getResources(), R.drawable.droid_1);
    ....
}
半透明的墙 2024-12-12 21:27:17

确保您的图像不在 drawable-v24 文件夹中,将其移动到 drawable 文件夹中。

这对我有用。

make sure your image is not in drawable-v24 folder, move it to drawable folder.

this worked for me.

梦情居士 2024-12-12 21:27:17

我的解决方案是创建一个可绘制对象(创建它没有任何问题),然后将其转换为位图

val drawable = context.getDrawable(context.resources, R.drawable.droid_1)
val bitmap = drawable.toBitmap(width = 100, height = 100, config = null)

您还可以使用drawable.widthdrawable.height来获得相同的比例和分辨率

My solution is to create a drawable (I had no issues creating it), then convert it to a bitmap

val drawable = context.getDrawable(context.resources, R.drawable.droid_1)
val bitmap = drawable.toBitmap(width = 100, height = 100, config = null)

You can also use drawable.width and drawable.height to have the same scale and resolution

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文