android pass media data to another activity [duplicate]

99封情书 提交于 2019-12-18 07:14:10

问题


Possible Duplicate:
Passing image from one activity another activity

my app uses following logic: a button click in Activity A starts the phone camera, after a picture/video is taken (the user pressed "save" in the camera window) Activity B starts. That Activity B contains a preview of taken picture/video and the possibility to upload the media data via a http request. I'm not sure how to pass the taken Image/Video to Activity B.. I can't launch the camera with StartActivityForResult in Activity A since the result must be delivered to Activity B. Any ideas ho to do that?


回答1:


There are 3 Solutions to solve this issue.

1) First Convert Image into Byte Array and then pass into Intent and in next activity get byte array from Bundle and Convert into Image(Bitmap) and set into ImageView.

Convert Bitmap to Byte Array:-

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

Pass byte array into intent:-

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

Get Byte Array from Bundle and Convert into Bitmap Image:-

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) First Save image into SDCard and in next activity set this image into ImageView.

3) Pass Bitmap into Intent and get bitmap in next activity from bundle, but the problem is if your Bitmap/Image size is big at that time the image is not load in next activity.




回答2:


One: First Save image into SDCard and in next activity set this image into ImageView.

Two: You can as well save into a byte array and pass it to next activity:

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

Then in the second intent get Byte Array from Bundle and Convert into Bitmap Image.



来源:https://stackoverflow.com/questions/13837720/android-pass-media-data-to-another-activity

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!