-->

机器人传球的媒体数据到另一个活动[重复](android pass media data to an

2019-07-05 00:32发布

可能重复:
从一个活动的另一个活动图像传递

我的应用程序使用以下逻辑:按钮点击在活动A开始电话照相机,图像/视频拍摄后(用户按下在照相机窗口“保存”)活动B开始。 该活动B包含拍摄的照片/视频,并且经由HTTP请求上载的媒体数据的可能性的预览。 我不知道如何通过所拍摄的图像/视频到活动B ..我不能StartActivityForResult在活动一推出的相机,因为结果必须被传递到活动B.任何浩这样做的想法?

Answer 1:

有3个解决方案来解决这个问题。

1)首先转换成图像字节数组然后通入意向和在下一个活动获得从包字节数组,转换为图像(位图),并设置成ImageView的。

位图转换到字节数组: -

Bitmap bmp = BitmapFactory.decodeResource(getResources(), R.drawable.ic_launcher);
ByteArrayOutputStream stream = new ByteArrayOutputStream();
bmp.compress(Bitmap.CompressFormat.PNG, 100, stream);
byte[] byteArray = stream.toByteArray();

通过字节数组到意图: -

Intent intent = new Intent(this, NextActivity.class);
intent.putExtra("picture", byteArray);
startActivity(intent);

从包中获取字节数组转换成位图图像: -

Bundle extras = getIntent().getExtras();
byte[] byteArray = extras.getByteArray("picture");

Bitmap bmp = BitmapFactory.decodeByteArray(byteArray, 0, byteArray.length);
ImageView image = (ImageView) findViewById(R.id.imageView1);

image.setImageBitmap(bmp);

2)先保存图像到SD卡,并在接下来的活动设置此图片为ImageView的。

3)合格的位图到意向并从包下一个活动得到的位图,但问题是,如果你的位图/图像大小是当时的图像不会在接下来的活动负载大。



Answer 2:

一:先保存图像到SD卡,并在接下来的活动设置此图片为ImageView的。

二:你可以和保存到一个字节数组,并把它传递给下一个活动:

    Intent A1 = new Intent(this, NextActivity.class);
    A1.putExtra("pic", byteArray);
    startActivity(A1);

然后在第二个意图从包中获取字节数组转换成位图图像。



文章来源: android pass media data to another activity [duplicate]