aspose file tools*
The moose likes Android and the fly likes Get byte[] of image Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Mobile » Android
Bookmark "Get byte[] of image" Watch "Get byte[] of image" New topic
Author

Get byte[] of image

yash wardhan
Ranch Hand

Joined: Jan 22, 2008
Posts: 46
Hi All,
I am working on an app. in which I set some images in gridview.
What I want to do is take the byte[] of any image clicked and store it in DB.
Currently I have stored those images in drawable of the application. I tried "Bitmap bitmap = view.getDrawingCache();", but it is giving bitmap as null.


Can anyone please help me in getting byte[] of the image?


Thanks,
Yash
yash wardhan
Ranch Hand

Joined: Jan 22, 2008
Posts: 46
Any replies on this please.
Monu Tripathi
Rancher

Joined: Oct 12, 2008
Posts: 1369
    
    1

[I am not sure I understood your question fully..]

If you have a handle on a android.graphics.Bitmap, you can write it to a java.lang.ByteArrayOutputStream using the compress method of android.graphics.BitmapFactory class. You can then get the byte array from java.lang.ByteArrayOutputStream object using toByteArray() method.

Something like,



Hope that helps..

[List of FAQs] | [Android FAQ] | [Samuh Varta]
yash wardhan
Ranch Hand

Joined: Jan 22, 2008
Posts: 46
Thanks Monu,

I tried this but the problem is my bm object is null.
Actually I have my images in drawable and I want to get the Bitmap (or byte[]) of the user selected image.
Don't know why object of bitmap is null.
I used getDrawingCache() of view, and I have one more doubt even if this method gives me bitmap, what if any user disables the cache(don't know how to do it though).

Please reply
Monu Tripathi
Rancher

Joined: Oct 12, 2008
Posts: 1369
    
    1

I'd imagine you can get selected bitmap by having a onItemClickListener on your grid view and overriding the onItemClick method(atleast if you have setup your adapter for grid view like this one here).

yash wardhan
Ranch Hand

Joined: Jan 22, 2008
Posts: 46
Monu,
I tries that solution but may be doing something wrong, can you please post the code.
I tried the same code that you pointed to. I want to convert that byte[] into string and save in DB.


Thanks,
Yash
Monu Tripathi
Rancher

Joined: Oct 12, 2008
Posts: 1369
    
    1

Sorry, I haven't coded anything in this regard. I was just making some general suggestions..

Why dont you go ahead and post your code-maybe that would help?
yash wardhan
Ranch Hand

Joined: Jan 22, 2008
Posts: 46
Hi Monu,

I tried following code which is not working.
The adapter is same as given in sample code here



In onItemClick() method bitmap is remaining null.
Monu Tripathi
Rancher

Joined: Oct 12, 2008
Posts: 1369
    
    1

Yash,

If you understand the purpose of all the functions in your ImageAdapter class and the way Adapters and views work, your problem is solved.
Here's what you can do:

Override the getItem(int position) method of the ImageAdapter class to return the resource Id.



In the onItemClick(..) of your TestGridImages do a getItem on the adapter, this will return the resource Id(remember we did so earlier).Use this resource Id to create a bitmap, then write the bitmap to your ByteArrayOutputStream.




 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Get byte[] of image