从的字节数组显示图像 [英] display image from byteArray
问题描述
问问题之前,让我告诉你关于我的类层次结构。我有一个主
类有的onCreate()
方法。在这种方法我已经做了映射文件类的object.and称为它的 READFILE()
和 readIndex()
的方法。在 readIndex()
方法,我调用另一个类名为 MapTile
在这里,我读了我的二进制文件中的图像砖和有我要显示我的形象。
before asking question let me tell you about my class hierarchy. i have a Main
class with has onCreate()
method. in that method i have made the MapFile class' object.and called its readFile()
and readIndex()
methods. in readIndex()
method i call another class named MapTile
where i read the images tiles from my binary file and there i have to display my image.
现在的问题是,我怎么能显示图像而没有把我的code到的onCreate(包savedInstanceStare)
method.i在尝试这种方式,但在第一行它给我空指针异常。
now question is how can i display image without putting my code into onCreate(Bundle savedInstanceStare)
method.i am trying this way but on first line it gives me null pointer exception.
ImageView image = (ImageView) findViewById(android.R.id.icon);
Bitmap bMap = BitmapFactory.decodeByteArray(imageTile, 0, imageTile.length);
image.setImageBitmap(bMap);
在此先感谢。
推荐答案
我觉得你的问题不是的字节数组,但 findViewById
。正如你说,NPE是在第一行。有规则解决这个方法,你有两个选择来调用它:
I think your issue is not the byteArray but the findViewById
. As you say that the NPE is on the first line. There are rules around this method you have two options to call it :
无论你用它来查询查看你已经在你的的setContentView
称为布局
或者你包含在布局视图你手动布局气筒充气使用它
Either you use it to query a View you already have in the layout you called in setContentView
Or you use it on a View contained in a layout you inflated manually with a layout inflater
如果您尝试使用它在你的活动中调用的setContentView
,你有没有夸大自己从不是一个任何其他布局视图,它会返回null。
If you try to use it in your activity to call a View from any other layout than the one in setContentView
that you have not inflated yourself, it will return null.
这篇关于从的字节数组显示图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!