5

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)

Nicolas Filotto
  • 43,537
  • 11
  • 94
  • 122
hatib abrar
  • 123
  • 3
  • 14

2 Answers2

4

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);
Nicolas Filotto
  • 43,537
  • 11
  • 94
  • 122
0

You want to convert ArrayList to byte[] ? or Object to byte[]?

I wrote in this way, just simply convert the element in ArrayList into byte[] ,it works!

    List<Object> objects = new ArrayList<Object>();

    objects.add("HelloWorld".getBytes());

    byte[] bytes = (byte[]) objects.get(0);

    System.out.println(new String(bytes));  // HelloWorld
hijawa
  • 31
  • 2