Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/412.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android:BitmapFactory.decodeResource返回null_Android_Graphics_Bitmap_Android Context - Fatal编程技术网

Android:BitmapFactory.decodeResource返回null

Android:BitmapFactory.decodeResource返回null,android,graphics,bitmap,android-context,Android,Graphics,Bitmap,Android Context,我似乎无法理解这一点。我有两个具有不同特征的java类,每个类调用BitmapFactory.decodeResource来获取相同的图像资源,一个返回位图,另一个返回null。两个类都在同一个包中 这是一个可以工作的类,它调用BitmapFactory.decodeResource,返回位图。我只包含了相关代码 package advoworks.test; import android.content.Context; import android.graphics.Bitmap; imp

我似乎无法理解这一点。我有两个具有不同特征的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在调试中返回空值。我只包含了我认为相关的代码

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");
    }
}
有任何线索吗?

这个
getResources()
是一个
Context
类方法,您没有在片段类中使用Context。它是如何工作的。您应该调用
getApplicationContext().getResources()

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

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

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

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


这对我来说很有效。

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

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

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

谢谢您的回复,您能解释一下“上下文类方法”是什么意思吗?我在google上找不到定义。
getResources()
Context
类的成员。所以需要上下文对象来调用该方法。那么为什么getResources不会导致编译错误呢?无论如何,我尝试过这样做:无论如何,我尝试过通过扩展类应用程序来获得这样的“上下文”:公共类段扩展应用程序{
public段(intx,inty){

try{

Context Context;

Context=getApplicationContext();

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


Log.d(标记,“错误为”+e);

}
}
很抱歉不习惯使用小标记。在context=getApplicationContext()处代码失败;返回的错误是java.lang.NullPointerException获取上下文的最佳方法是什么?您在logcat中是否有任何错误?不,我在logcat中没有任何错误:(为什么你需要在同一个应用程序中加载同一资源两次。加载一次并将其引用传递到所有你需要的地方。谢谢!这也是我的问题。)