hatib abrar hatib abrar - 2 months ago 5
Android Question

Convert Arraylist<Object> value in to byte[]

I m trying to get/convert the value of Arraylist in byte[]
below is my code

final ArrayList<Object> imglists = new ArrayList<Object>();


this is my arraylist of Objects in this arraylist m storing the values of images in form of bytes

for (int i=0; i<mPlaylistVideos.size();i++) {
holder.mThumbnailImage.buildDrawingCache();
Bitmap bitmap= holder.mThumbnailImage.getDrawingCache();
ByteArrayOutputStream bs = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.JPEG, 50, bs);
byte[] rough = bs.toByteArray();
imglists.add(i,rough);
}


I m trying to get the specific value from arraylist and store that in byte[]
this is what I was trying to do

byte[] value=imglists.get(2);


I could not find any complete answer to convert Arraylist of Object into byte[]
I know Arraylist doesn't support primitive datatype (i-e byte)

Answer

What you are looking for is a List of byte[], something like that:

List<byte[]> imglists = new ArrayList<>();

Then you can simply add your byte array to your List using the add(E) method as next:

imglists.add(bs.toByteArray());

You will then be able to access to a given byte array from its index in the List using the method get(int) as you try to achieve:

// Get the 3th element of my list
byte[] value = imglists.get(2);