简体   繁体   English

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

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

Any ideas how I can extract an image from a JSON object and pass to another Activity via intent to another object? 关于如何从JSON对象提取图像并通过意图传递给另一个对象的任何想法? So far this is as closest I've got is recieving a byte array in the recieving activity, but BitmapFactory.decodeByteArray returns null. 到目前为止,这与我在接收活动中接收字节数组是最接近的,但是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);

In the receiving fragment: 在接收片段中:

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 returns null bmp返回null

Update 更新资料

I messed up, the JSON JsonNode attachmentText = item.get("_attachments"); 我搞砸了,JSON JsonNode attachmentText = item.get("_attachments"); wasn't returning an image but the image meta data instead. 不是返回图像,而是图像元数据。

I've tried going a different route and converting an ImageView from the view layout to a byte array and passing that to the activity instead, but that doesn't work either, bmp returns null: 我尝试过不同的路线,并将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);

Is the only thing left to do query the database again for the images I've already retrieved? 剩下的唯一要做的事情是再次查询数据库以获取我已经检索到的图像吗? Currently they are in a listView, and im trying to get the selected list items image to show in the detail view attached to the new activity. 当前它们在listView中,并且我试图获取选定的列表项图像以显示在附加到新活动的详细信息视图中。

Here's the working code which originally retrieves the images for the listview: 这是最初为listview检索图像的工作代码:

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

Once you know you have the image JSON string ... 一旦知道了图像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