• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Jeanne Boyarsky
  • Liutauras Vilda
  • Campbell Ritchie
  • Tim Cooke
  • Bear Bibeault
Sheriffs:
  • Paul Clapham
  • Junilu Lacar
  • Knute Snortum
Saloon Keepers:
  • Ron McLeod
  • Ganesh Patekar
  • Tim Moores
  • Pete Letkeman
  • Stephan van Hulst
Bartenders:
  • Carey Brown
  • Tim Holloway
  • Joe Ess

Byte[] to image  RSS feed

 
Ranch Hand
Posts: 136
Android Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Good morning,
I have a .Net web service that return a byte[](actually its an image). How can i read this byte[] array and in code convert it to an image an display it in a ImageView.

thanks in advance.
 
Rancher
Posts: 42975
76
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What image format does the byte[] contain?

In some way the ImageView.setImageBitmap(Bitmap) method will probably be involved, so you should read up on how to create suitable Bitmap objects; the setPixel/setPixels methods seem particularly relevant.
 
Aris Doxakis
Ranch Hand
Posts: 136
Android Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
its a jpeg file. my problem is that i dont know how to extract it from the web service. my code for retrieving the image is
 
Ulf Dittmer
Rancher
Posts: 42975
76
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Doesn't "bytearray" contain a valid image? You should check that before you proceed. If it doesn't, then lines 16 and 17 don't do the right thing.

Once you have a valid byte[], it seems that BitmapFactory.decodeByteArray can convert that into a Bitmap object.
 
Aris Doxakis
Ranch Hand
Posts: 136
Android Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i finally got the code to work. but a weird thing is going on, on the emulator this code
try
{
byte[] myImage = gemyImage();

Bitmap bmp=BitmapFactory.decodeByteArray(myImage,0,myImage.length);
if (bmp!=null)
{
ImageView myView = (ImageView)findViewById(R.id.myImage);
myView.setImageBitmap(bmp);
}
}
catch (Exception e)
{
Log.e("Create file error : ",e.getMessage());
}
that displays the image that i get from the webservice works, when i copy the app to a device with 2.1 on it just crashes. no errors can be viewed. any idea why this is happening?

thanks in advance.
 
Ulf Dittmer
Rancher
Posts: 42975
76
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

...it just crashes. no errors can be viewed. any idea why this is happening?


This sounds odd. Whenever Android sees fit to terminate an application there should always be a message in the logcat output. Which lines of the code you just posted do get executed in the case of the app crashing?
 
Aris Doxakis
Ranch Hand
Posts: 136
Android Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
everything else n the app, which is mostly getting data from web services works. When i try to get the image and display it it crashes. The weird thing is that on the emulator it works fine. On my device i dont know what happens. can i test my app on my device and not on the emulator from eclipse? cant see on which line it is crashing because its running on my device...

thanks again
 
Ulf Dittmer
Rancher
Posts: 42975
76
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

cant see on which line it is crashing because its running on my device...


Sure you can, that's what the logcat tool is for that comes with the Android SDK.
 
Aris Doxakis
Ranch Hand
Posts: 136
Android Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i think i found what was crashing the app, the emulator has 2.2 version, my device has 2.1 it throws this error
02-24 12:13:42.221: ERROR/AndroidRuntime(341): java.lang.NoClassDefFoundError: android.util.Base64

cant i use the Base64 on older versions?? is there an other utility for older versions?


 
Ulf Dittmer
Rancher
Posts: 42975
76
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Try this: http://www.source-code.biz/base64coder/java/Base64Coder.java.txt

And, of course, properly select and set the Android SDK version you want to support in your app. Android 2.1 is still quite widespread at this time, so you may want to require at most API level 7.
 
Aris Doxakis
Ranch Hand
Posts: 136
Android Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thanks for the help. got the code. didnt think that it would have that much difference between the 2 versions.. thanks again.
 
Don't get me started about those stupid light bulbs.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!