简体   繁体   English

Android 中的 PNG 图片和 InputStream

[英]PNG Images and InputStream in Android

I need to decode an .png image to use it as a base to create some Rects and to use the image itself as a background for my application, but the try-catch keeps throwing me a Null Pointer Exception, this is the code:我需要解码一个 .png 图像以使用它作为基础来创建一些 Rects 并将图像本身用作我的应用程序的背景,但是 try-catch 不断向我抛出空指针异常,这是代码:

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

    }

I've tried some logs and the try-catch break right on the InputStream line and prints the 'Log.d' in catch block, I checked the file name and it is alright.我已经尝试了一些日志,并且 try-catch 在 InputStream 行上中断并在 catch 块中打印了“Log.d”,我检查了文件名,一切正常。

您可以直接在decodeStream使用getAssets.open方法。

figure = BitmapFactory.decodeStream(getAssets.open("background.png"));

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM