Display byte array of image in imageview - android
I am trying to show byte array of image from server inside the image view of an android activity. I am able to get the byte array correctly as sent from the server, but while converting it into bitmap it is always returning null. I have used the BitmapFactory.decode(byteArray,0, byteArray.length) for converting image byte array to bitmap which is returning null always.
Please help me solving this and tell me if there are any alternative.
Thanks in advance.
Well a better option is to use this. I used it to display images that is present on a server. Pretty fast
URL url = new URL(link);
InputStream picin = url.openStream();
Bitmap myBitmap = BitmapFactory.decodeStream(picin);
pic.setImageBitmap(myBitmap);
Where link points to the jpg image. Works for other image formats also.
Related
Cocos2dx: Garbled bitmap image from android
Note: Using the latest of cocos 2.x Hi there. I'm trying to pass a bitmap through jni and convert it to a sprite to display, but the image created using CCTexture2d's initWithData is completely garbled. I've looked at other forum posts here, but even after following those as closely as possible, nothing works. Note that we are not going to use a path for the image (and it isn't an option). There are a few issues that I will break down: What kind of data is necessary? The initWithData method has no documentation as to what kind of data it even takes- byte array? Pixel array? It seems other people have used byte array and got it working, so that is what we have gone with. This is the code to get the byte array, but are we supposed to use PNG or JPEG format? ByteArrayOutputStream bAOS = new ByteArrayOutputStream(); bitmap.compress(Bitmap.CompressFormat.PNG,100, bAOS); byte[] bytes = bAOS.toByteArray(); Getting the data to cocos We are using some other convenience methods to write jni messages as json data which gets converted to a CCDictionary. The Byte array gets turned into a CCArray of a bunch of ints, which we can then loop through and assign...to what kind of array? "byte" isn't a valid primitive in c++. Online I've seen an int array: CCArray *bytes = (CCArray*)dictionary->objectForKey("bytes"); int byteArray[bytes->count()]; for (int i = 0; i < bytes->count(); i++) { CCNumber *v = (CCNumber*)bytes->objectAtIndex(i); byteArray[i] = v->getIntValue(); }; and I've tried a char array as well, but neither worked. CCTexture2d? CCImage? Ok, so now we have our supposed byte array, we can render the image. Do we use CCTexture2d or CCImage? For CCTexture2d (we know the image is 20x20 px): CCTexture2D *texture = new CCTexture2D(); texture->initWithData(byteArray,kCCTexture2DPixelFormat_RGBA8888,20,20,CCSizeMake(20,20)); The image is garbled. For CCImage...what is the length? I've seen length calculated as the image witdth * image height, or as the actual length of the byte array. Your guess is as good as mine, as I have never gotten a CCImage to work. CCImage *image = new CCImage(); image->initWithImageData(byteArray,length,CCImage::kFmtPng,20,20,8); Once we have a CCImage, we can set it to a texture using CCTexture2d's initWithImage function. Now that we have the Texture, making a sprite with it should be simple: CCSprite *sprite = CCSprite::createWithTexture(texture); Which works, in as much as a sprite is created. The image is trash though. So, what exactly is wrong here? How do we get the data from the bitmap? How do we make the data something that cocos can render? Texture2d or Image? What are the parameters? For kicks, here is the image we are testing: And here is the byte array as Android reports it: [-119,80,78,71,13,10,26,10,0,0,0,13,73,72,68,82,0,0,0,40,0,0,0,40,8,2,0,0,0,3,-100,47,58,0,0,0,3,115,66,73,84,8,8,8,-37,-31,79,-32,0,0,0,-71,73,68,65,84,88,-123,-19,-44,-79,13,-125,48,16,-123,-31,-97,8,-118,-48,96,37,-108,40,-70,34,3,-48,-91,-124,81,24,-115,77,48,27,-64,6,-98,32,50,19,-112,5,48,-123,69,20,41,-36,-55,-35,-45,-13,-41,-100,-114,21,-30,-34,64,-45,48,-60,-74,-41,11,63,26,-123,21,86,88,97,-123,21,86,-8,-60,112,106,-101,-56,-90,-61,11,83,48,-10,6,39,44,38,-108,39,-51,16,9,11,69,-117,8,-127,-81,-89,-102,-66,99,-82,67,-11,116,108,35,97,88,-124,121,-81,109,-4,78,120,-66,-27,82,88,-31,-81,77,26,-35,-12,20,19,66,-32,-128,92,51,-71,21,46,47,-19,-15,-80,67,122,58,-61,-10,109,-86,114,-9,122,-40,-22,-19,-114,-121,23,-52,76,13,-19,102,-6,-52,-20,-67,112,73,57,-122,-22,-25,91,46,-123,21,-2,63,-8,3,120,-72,71,35,-19,75,10,-28,0,0,0,0,73,69,78,68,-82,66,96,-126] And here is the result (the image is not 20 x 20 though- scaled by cocos to fit density, probably) Any help would be greatly appreciated
Downloading a png image
I need to retrieve a png image from the Internet. I have the correct URL pointing to the image, it's something like this: So, I have my URL object like this: URL url = new URL(methodThatLoadsTheStringPointingToMyURL()); An I can get a InputStream like this: InputStream is = url.openStream(); However, the only way I know to decode it into a Drawable is by using BitmapDrawable constructor, and that kills the PNG transparency. Is there any way to retain PNG transparency?
http://www.libpng.org/pub/png/libpng.html The able link must help you out to address the problem of retaining the Transparency. Its a standard Library to read and write png image data
Send byte array of a Bitmap using http post in android?
In my application I need to call an API Using Http Post to send some information to the web server. So I need to append userid, name, phoneNo and image to the url. I need to convert the image to a byte array and append it to url. I used the following code but it didn't work for me. I just got a byte array like this [B#4055dd90 My code is Bitmap bitmap1 = ((BitmapDrawable) d).getBitmap(); ByteArrayOutputStream stream = new ByteArrayOutputStream(); bitmap1.compress(Bitmap.CompressFormat.PNG, 100, stream); byte[] bitmapdata = stream.toByteArray(); Can Some one please help Me ? Thanks in advance !
Two things: Images can get pretty big. So, you may be better off using a JPG, as you can compress it (a PNG does not compress). So, you'd use Bitmap.CompressFormat.JPEG and set the amount of compression you want. When sending in your post, you should encode the byte[] like this: Base64.encodeBase64String( bitmapdata); Let me know if that works for you. OH, and don't forget to unencode it on your server side.
You can not send the images in the byte array form directly to server. You have to encode it into Base64 String with the help of Base64 Class. Encode Bitmap byte array to Base64 string and send that string to server with the help of HTTPPost method. If you have any doubt then you can comment.
The image is too small when I try to upload it using Facebook Graph API and Android
Simply put I need to capture image using camera and upload it to facebook via my android application. And I successfully did that. The problem is when the photo posted in facebook, it's just too small and in low resolution while the image I took is in high resolution. I understand that: in order to upload to facebook, i need to convert the captured image which is in bitmap format into byte array. So i have method for that: public static byte[] convertBitmapToByteArray(Bitmap bm){ ByteArrayOutputStream bos = new ByteArrayOutputStream(); bm.compress(CompressFormat.PNG, 100, bos); byte[] bitmapdata = bos.toByteArray(); return bitmapdata; } Then to upload the image to facebook, i have code below where byteData is byte array I converted from bitmap image using the method above. parameters.putString("message", "Test"); parameters.putByteArray("source", byteData); String facebookResponse = facebookInstance.request(albumId+"/photos",parameters,"POST"); return facebookResponse; I am pretty sure the problem is my convertBitmapToByteArray method since the method is to compress the bitmap image and turn it into byte array, and this made my image into low resolution image. However I can't seem to find the way to upload the image without converting it into byte array first. Any solution for me?
Alright even this thread is old, i found out the answer. It's not the problem of CompressFormant.JPEG or CompressFormat.JPG. Simply put, intent in android isn't designed to carry big data like image from activity through activity. I need to save the image from intent to sd card first before able to pull it out from there. It's my solution.
get image from remote server
i have image in sqlserver db.using ksoap2 client webservice for getting image.there i am converting image into base64 encoding and in mobile i am converting it into base64 decoding but i am unable to get image whats the problem.how i will get image or any alternative solution for this problem Thanks in advance Aswan
finally i got solution for my problem by creating custom base64 class
If you decode from base64 what you get should be technically an image file and cannot be used directly in the code. Maybe what you are looking for is something like Image i = BitmapFactory.decodeByteArray(data, offset, length); Where "data" is a byte array you have after the decoding, offset should be set to 0 and length equal to data.length. After that, you can use the image with your views.