簡體   English   中英

轉換Arraylist <Object> 值到byte []

[英]Convert Arraylist<Object> value in to byte[]

我想在下面的byte []中獲取/轉換Arraylist的值是我的代碼

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

這是我在這個arraylist m中的對象的arraylist,以字節的形式存儲圖像的值

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);
}

我試圖從arraylist獲取特定值並將其存儲在byte []中這就是我想要做的事情

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

我找不到將Object的Arraylist轉換為byte []的完整答案我知道Arraylist不支持原始數據類型(即byte)

您正在尋找的是一個byte[] List ,類似於:

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

然后,您可以使用add(E)方法將您的byte array添加到List中,如下所示:

imglists.add(bs.toByteArray());

然后,當您嘗試實現時,您將能夠使用get(int)方法get(int) List中的索引訪問給定的byte array

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

您想將ArrayList轉換為byte []嗎? 或者對象為byte []?

我用這種方式編寫,只需將ArrayList中的元素轉換為byte [],就可以了!

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

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

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

    System.out.println(new String(bytes));  // HelloWorld

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM