安卓:BitmapFactory.decodeResource返回null

15

我似乎无法解决这个问题。我有两个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");
    }
}

有任何线索吗?


你在logcat中有看到任何错误吗? - blessanm86
没有在 logcat 中找到任何错误 :( - Kevin
为什么在同一个应用程序中需要两次加载相同的资源。只需加载一次并将其引用传递到需要它的所有位置。 - Ron
4个回答

7

请检查您图片的分辨率,如果太大,BitmapFactory.decodeResource将返回空值(不会抛出异常)


4
getResources()是一个Context类的方法,而你的Segment类中没有使用任何Context。那么它该如何工作呢?你应该调用getApplicationContext().getResources()
你需要将context传递给Segment构造函数。
public Segment(Context context, int x, int y) {
    ....
    bitmap = BitmapFactory.decodeResource(context.getResources(), R.drawable.droid_1);
    ....
}

1
谢谢您的回复,您能解释一下“上下文类方法”是什么意思吗?我在谷歌上找不到定义。 - Kevin
1
getResources()Context 类的成员。因此需要上下文对象来调用该方法。 - Ron
为什么getResources不会导致编译错误呢?无论如何,我已经尝试过这样做: - Kevin
无论如何,我已经尝试通过扩展Application类来获取像这样的“Context”:public class Segment extends Application {<br/> public Segment(int x, int y) {<br/> try {<br/> Context context;<br/> context = getApplicationContext();<br/> this.bitmap = BitmapFactory.decodeResource(context.getResources(), R.drawable.droid_1);<br/> } catch (Exception e) {<br/> Log.d(TAG,"错误是 " + e);<br/> } } - Kevin
抱歉,我不习惯使用迷你标记。代码在 context = getApplicationContext(); 处失败。 返回的错误是 java.lang.NullPointerException。 获取上下文的最佳方法是什么? - Kevin
你在哪里使用你的段落类?那里有上下文吗?我已经更新了答案。 - Ron

3

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

对我来说这样有效。


1
谢谢!这也是我的问题。 - Eugenio Lopez

-2
我的解决方案是创建一个drawable(我创建它时没有任何问题),然后将其转换为位图。
val drawable = context.getDrawable(context.resources, R.drawable.droid_1)
val bitmap = drawable.toBitmap(width = 100, height = 100, config = null)

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


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接