【发布时间】:2013-03-25 15:32:56
【问题描述】:
任何想法如何从 JSON 对象中提取图像并通过意图传递给另一个 Activity 到另一个对象?到目前为止,这与我在接收活动中接收字节数组最接近,但 BitmapFactory.decodeByteArray 返回 null。
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);
剩下的唯一事情是再次查询数据库以获取我已经检索到的图像吗?目前他们在一个列表视图中,我试图让选定的列表项图像显示在附加到新活动的详细视图中。
这是最初为列表视图检索图像的工作代码:
AttachmentInputStream readAttachment = db.getAttachment(row.getId(), imgId);
Bitmap bitmap = BitmapFactory.decodeStream(readAttachment);
img.setImageBitmap(bitmap);
【问题讨论】:
-
你确定你成功获取了bytearray?你试过记录字节数组长度吗?
-
你说得对,这是我最近的尝试,它甚至不返回字节数组,我会更新我的问题,谢谢
-
现在您已经成功获取字节数组,然后再传递给另一个活动?
-
是的,我已经检查了接收片段中的 byteArray 并且它通过了 ok
-
所以我想问题现在解决了?