Android 显示来自 byteArray 的图像

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3520019/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 10:35:50  来源:igfitidea点击:

display image from byteArray

androidarraysimage

提问by sajjoo

I have a Mainclass 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 MapTilewhere i read the images tiles from my binary file and there i have to display my image.

我有一个Main带有onCreate()方法的类。在该方法中,我创建了 MapFile 类的对象。readFile()并调用了它的和readIndex()方法。在readIndex()方法中,我调用另一个名为的类MapTile,从我的二进制文件中读取图像图块,然后我必须显示我的图像。

Question is, how can I display an image without putting my code into onCreate(Bundle savedInstanceStare)method? I am trying this way but on first line it gives me NullPointerException.

问题是,如何在不将代码放入onCreate(Bundle savedInstanceStare)方法的情况下显示图像?我正在尝试这种方式,但在第一行它给了我NullPointerException

ImageView image = (ImageView) findViewById(android.R.id.icon);           
Bitmap bMap = BitmapFactory.decodeByteArray(imageTile, 0, imageTile.length);
image.setImageBitmap(bMap);

采纳答案by Sephy

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 :

我认为您的问题不是 byteArray 而是findViewById. 正如您所说,NPE 在第一行。围绕此方法有一些规则,您有两种选择可以调用它:

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
或者您在使用布局充气器手动膨胀的布局中包含的视图上使用它

If you try to use it in your activity to call a View from any other layout than the one in setContentViewthat you have not inflated yourself, it will return null.

如果您尝试在您的活动中使用它从任何其他布局调用视图,而不是setContentView您自己没有膨胀的布局,它将返回 null。

回答by bsautner

adding a byte array to an android imageview:

将字节数组添加到 android imageview:

        //byte[] chartData 
        ImageView imgViewer = (ImageView) findViewById(R.id.chart_image);
        Bitmap bm = BitmapFactory.decodeByteArray(chartData, 0, chartData.length);
        DisplayMetrics dm = new DisplayMetrics();
        getWindowManager().getDefaultDisplay().getMetrics(dm);

        imgViewer.setMinimumHeight(dm.heightPixels);
        imgViewer.setMinimumWidth(dm.widthPixels);
        imgViewer.setImageBitmap(bm);