【问题标题】:Pass image via ByteArray to another Activity - Android通过 ByteArray 将图像传递给另一个 Activity - Android
【发布时间】: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
  • 所以我想问题现在解决了?

标签: java android


【解决方案1】:

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

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-08-30
    • 1970-01-01
    • 1970-01-01
    • 2014-07-24
    • 2011-07-24
    • 1970-01-01
    • 2017-04-05
    相关资源
    最近更新 更多