2013-03-25 64 views
0

任何想法如何從JSON對象提取圖像並通過意圖傳遞給另一個Activity到另一個對象?到目前爲止,這是最接近我得到的是接收活動中的字節數組,但BitmapFactory.decodeByteArray返回null。通過ByteArray傳遞圖像到另一個Activity - Android

public void onItemClick(AdapterView<?> parent, View view, int position, long id) { 
       DetailFragment fragB = (DetailFragment) getSupportFragmentManager() 
         .findFragmentById(R.id.detail_frag); 

        Row row = (Row)parent.getItemAtPosition(position); 
        JsonNode item = row.getValueAsNode(); 
        intent.putExtra("item", item.toString()); 

        JsonNode attachmentText = item.get("_attachments"); 

        //hidden code which gets the imgId from the JsonNode 

        byte[] byteArray = attachmentText.get(imgId).toString().getBytes(); 

        intent.putExtra("picture", byteArray); 

        startActivity(intent); 

在接收片段:

byte[] byteArray = getArguments().getByteArray("picture"); 

Bitmap bmp = BitmapFactory.decodeByteArray(byteArray, 0, byteArray.length); 
ImageView image = (ImageView) view.findViewById(R.id.imgDetail); 

image.setImageBitmap(bmp); 

BMP返回null

更新

我搞砸了,在JSON JsonNode attachmentText = item.get("_attachments");沒有回的圖像,但圖像元數據代替。

我試着去不同的路線,並從視圖佈局轉換的ImageView的字節數組,並傳遞一個以活動代替,但也不管用,BMP返回null:

 ImageView image = (ImageView) view.findViewById(R.id.img); 

Bitmap bmp = BitmapFactory.decodeResource(getResources(), image.getId()); 
ByteArrayOutputStream stream = new ByteArrayOutputStream(); 
bmp.compress(Bitmap.CompressFormat.PNG, 100, stream); 
byte[] byteArray = stream.toByteArray(); 

//pass Byte Array to intent 
intent.putExtra("picture", byteArray); 

是否只剩下爲我已經檢索的圖像再次查詢數據庫?目前他們在一個listView,並試圖讓選定的列表項圖像顯示在附加到新活動的詳細視圖。

這裏的最初獲取的圖像爲ListView的工作代碼:

AttachmentInputStream readAttachment = db.getAttachment(row.getId(), imgId); 
Bitmap bitmap = BitmapFactory.decodeStream(readAttachment); 
img.setImageBitmap(bitmap); 
+0

你確定你順利拿到字節組?你有沒有嘗試記錄字節數組的長度? – 2013-03-25 15:35:17

+0

你說得對,這是我最新的嘗試,甚至不返回字節數組,我會更新我的問題,謝謝 – KingFu 2013-03-25 16:14:07

+0

現在你已經成功獲得字節數組之前傳遞給另一個活動? – 2013-03-25 16:35:32

回答

0

一旦你知道你有像JSON字符串...

private Bitmap getBitmapFromString(String jsonString) { 
    byte[] decodedString = Base64.decode(stringPicture, Base64.DEFAULT); 
    Bitmap decodedByte = BitmapFactory.decodeByteArray(decodedString, 0, decodedString.length); 
    return decodedByte; 
} 
相關問題