Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

display image from byteArray

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.

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.

ImageView image = (ImageView) findViewById(android.R.id.icon);           
Bitmap bMap = BitmapFactory.decodeByteArray(imageTile, 0, imageTile.length);
image.setImageBitmap(bMap);
like image 539
sajjoo Avatar asked Aug 19 '10 08:08

sajjoo


People also ask

How do you convert Bytearrays to strings?

Convert byte[] to String (text data) toString() to get the string from the bytes; The bytes. toString() only returns the address of the object in memory, NOT converting byte[] to a string ! The correct way to convert byte[] to string is new String(bytes, StandardCharsets. UTF_8) .

What is Bytearray?

A byte array is simply a collection of bytes. The bytearray() method returns a bytearray object, which is an array of the specified bytes. The bytearray class is a mutable array of numbers ranging from 0 to 256.


2 Answers

adding a byte array to an 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);
like image 97
bsautner Avatar answered Sep 28 '22 16:09

bsautner


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 :

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

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.

like image 37
Sephy Avatar answered Sep 28 '22 18:09

Sephy