Java 通过ByteArray将图像传递给另一个活动-Android
你知道如何从一个JSON对象中提取一个图像,并通过另一个对象的意图传递给另一个活动吗?到目前为止,我得到的最接近的结果是在Receiving活动中接收字节数组,但BitmapFactory.decodeByteArray返回nullJava 通过ByteArray将图像传递给另一个活动-Android,java,android,Java,Android,你知道如何从一个JSON对象中提取一个图像,并通过另一个对象的意图传递给另一个活动吗?到目前为止,我得到的最接近的结果是在Receiving活动中接收字节数组,但BitmapFactory.decodeByteArray返回null public void onItemClick(AdapterView<?> parent, View view, int position, long id) { DetailFragment fragB = (Detai
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);
bmp返回null
更新
我搞砸了,JSONJsonNode 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检索图像的工作代码:
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;
}
你确定你成功地得到了bytearray吗?你试过记录字节数组长度吗?没错这是我最近的一次尝试,甚至没有返回字节数组,我会更新我的问题,谢谢你在传递到另一个活动之前已经成功获取字节数组了?是的,我已经在接收片段中检查了byteArray,它正在被传递好了,所以我想问题现在已经解决了?
private Bitmap getBitmapFromString(String jsonString) {
byte[] decodedString = Base64.decode(stringPicture, Base64.DEFAULT);
Bitmap decodedByte = BitmapFactory.decodeByteArray(decodedString, 0, decodedString.length);
return decodedByte;
}