Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Get byte[] of image

 
yash wardhan
Ranch Hand
Posts: 46
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 46
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Any replies on this please.
 
Monu Tripathi
Rancher
Posts: 1369
1
Android Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
[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..
 
yash wardhan
Ranch Hand
Posts: 46
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1369
1
Android Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 46
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1369
1
Android Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 46
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1369
1
Android Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.




 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic