我需要解码.png图像以将其用作创建一些Rects的基础并将图像本身用作我的应用程序的背景,但是try-catch不断给我一个Null Pointer Exception,这就是代码:
try {
InputStream is = ParamsSingleton.assetManager.open("background.png");
figure = BitmapFactory.decodeStream(is);
height = figure.getHeight();
width = figure.getWidth();
src = new Rect(0, 0, width, height);
first = new Rect();
} catch (IOException e) {
Log.d(TAG, "Image decode fail!");
}
我已经尝试了一些日志,并且在InputStream行上尝试了try-catch,并打印了' Log.d'在catch块中,我检查了文件名,它没问题。
答案 0 :(得分:1)
您可以在getAssets.open
内部直接使用decodeStream
方法。
figure = BitmapFactory.decodeStream(getAssets.open("background.png"));