繁体   English   中英

通过ByteArray将图像传递到另一个Activity-Android

[英]Pass image via ByteArray to another Activity - Android

关于如何从JSON对象提取图像并通过意图传递给另一个对象的任何想法? 到目前为止,这与我在接收活动中接收字节数组是最接近的,但是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);

剩下的唯一要做的事情是再次查询数据库以获取我已经检索到的图像吗? 当前它们在listView中,并且我试图获取选定的列表项图像以显示在附加到新活动的详细信息视图中。

这是最初为listview检索图像的工作代码:

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

一旦知道了图像JSON字符串...

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM