15

I am making an application in which i am getting an Mjpg Images from Live IP Camera in Android.

The problem is that since when i get the Image in the form of ByteArray. How can I display the Image?

I'm using the following code to display the image:

ImageView imageView = (ImageView) findViewById(R.id.imageView1);
Bitmap bmp = BitmapFactory.decodeByteArray(buffer, start, a);
imageView.setImageBitmap(bmp);

but i m getiing an exception

android.view.ViewRoot CallFromWrongThreadException

Please explain how to display the bytearray on android layout.

GSerg
  • 76,472
  • 17
  • 159
  • 346
Shah
  • 4,990
  • 10
  • 48
  • 70

5 Answers5

14
ImageView tv1;
tv1= (ImageView) findViewById(R.id.image);
InputStream si1 = asset.open("image/" + cat_arr1[i] + ".png");
Bitmap bitmap1 = BitmapFactory.decodeStream(si1);
tv1.setImageBitmap(bitmap1);
GSerg
  • 76,472
  • 17
  • 159
  • 346
Yogesh Tiwari
  • 149
  • 1
  • 4
6

You should use runOnUiThread() to update views from other threads.

Bertram Gilfoyle
  • 9,899
  • 6
  • 42
  • 67
adnorid
  • 106
  • 1
1

When you are doing some view related job always do it from GUI thread ie. the thread that created the view hierarchy .

So use Handlers to achieve this.

Tanmay Mandal
  • 39,873
  • 12
  • 51
  • 48
1

The exception CallFromWrongThreadException means that you tried to update the UI from a thread that is not the UI thread and which is not allowed.

So your issue is not that your code to create the image is wrong, but that you are doing it at the wrong place. If you e.g. are fetching the image in background in an AsyncTask, you need to setImageBitMap() in postExecute() and not in doInBackground().

Without seeing more code, we can't help you more.

Heiko Rupp
  • 30,426
  • 13
  • 82
  • 119
1
View mImg = findViewById(R.id.image_from_gallery);
((ImageView) mImg).setImageBitmap(bitmap);

This worked for me.

Pang
  • 9,564
  • 146
  • 81
  • 122
Satheesh18
  • 11
  • 1