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