aspose file tools*
The moose likes Android and the fly likes Byte[] to image Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Mobile » Android
Bookmark "Byte[] to image" Watch "Byte[] to image" New topic
Author

Byte[] to image

Aris Doxakis
Ranch Hand

Joined: Dec 05, 2004
Posts: 136

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.
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 42282
    
  64
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.


Ping & DNS - my free Android networking tools app
Aris Doxakis
Ranch Hand

Joined: Dec 05, 2004
Posts: 136

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
Marshal

Joined: Mar 22, 2005
Posts: 42282
    
  64
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

Joined: Dec 05, 2004
Posts: 136

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
Marshal

Joined: Mar 22, 2005
Posts: 42282
    
  64
...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

Joined: Dec 05, 2004
Posts: 136

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
Marshal

Joined: Mar 22, 2005
Posts: 42282
    
  64
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

Joined: Dec 05, 2004
Posts: 136

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
Marshal

Joined: Mar 22, 2005
Posts: 42282
    
  64
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

Joined: Dec 05, 2004
Posts: 136

thanks for the help. got the code. didnt think that it would have that much difference between the 2 versions.. thanks again.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Byte[] to image