简体   繁体   English

如何将图像数据从一个活动传递到另一个活动?

[英]How to pass image data from one activity to another activity?

I am using two activities.我正在使用两个活动。 One activity displays images in a GridView and by clicking on a particular image in that GridView it should display the full screen image in another activity.一个活动在GridView中显示图像,通过单击GridView中的特定图像,它应该在另一个活动中显示全屏图像。

How can I achieve this?我怎样才能做到这一点?

My MyGridView.java我的 MyGridView.java

mGridView.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View v, int position,long id) {
    // TODO Auto-generated method stub
    Toast.makeText(getApplicationContext(), "Image"+(position+1),Toast.LENGTH_SHORT).show();
    System.out.println(id);
    Intent i = new Intent(this, MyImageViewActivity.class);
    Bundle bundle = new Bundle();
    bundle.putInt("image", position);
    i.putExtras(bundle);
    startActivityForResult(i, 0);
}
});

Pass the image URL/Uri instead of passing raw image data.传递图像 URL/Uri 而不是传递原始图像数据。

You pass parameters to an Activity in an Intent.您将参数传递给 Intent 中的 Activity。 If the image comes from a file, pass the path String, otherwise pass the Bitmap如果图像来自文件,则传递路径字符串,否则传递 Bitmap

startActivity(new Intent(this, YourActivity.class).putExtras(new Bundle().putParcelable("bitmap", Bitmap)))

To pass the data between two activities:在两个活动之间传递数据:

bytes[] imgs = ... // your image
Intent intent = new Intent(this, YourActivity.class);
intent.putExtra("img", imgs);
startActivity(intent);

Then in YourActivity:然后在YourActivity中:

bytes[] receiver = getIntent().getExtra("imgs");

Also go thro this link which wil also help u.还有 go 通过这个链接也可以帮助你。
Here u can know how to convert bitmap to bytes在这里你可以知道如何将 bitmap 转换为字节

In MyGridView: (someInteger is an integer that represents the index of the selected image在 MyGridView 中:(someInteger 是一个 integer 表示所选图像的索引

Intent myIntent = new Intent(this, MyImageViewActivity.class);
Bundle bundle = new Bundle();
bundle.putInt("image", someInteger);
myIntent.putExtras(bundle);
startActivityForResult(myIntent, 0);

In MyImageViewActivity:在 MyImageViewActivity 中:

Bundle bundle = this.getIntent().getExtras();
int pic = bundle.getInt("image");

of course, you can put anything in the bundle!当然,你可以把任何东西放在包里! maybe a byte array or something也许是一个字节数组或其他东西

Once an item of Grid View is clicked, get the clicked item and pass it to next activity as an argument through PutExtra .单击Grid View的项目后,获取单击的项目并将其作为参数通过PutExtra传递给下一个activity In the next activity retrieve the image from extras and display it to user在下一个活动中,从 extras 中检索图像并将其显示给用户

I suppose you need use Intent class.我想你需要使用 Intent class。

Intent intent = new Intent(YourSourceActivity.this, TargetActivty.class);
Bundle addinfo = new Bundle();

addinfo.putInt("imageid", someid);

intent.putExtras(addinfo);

This is my process: it's so good.这是我的过程:太好了。 Activity1:活动1:

ByteArrayOutputStream stream=new ByteArrayOutputStream();
    bitmap.compress(Bitmap.CompressFormat.JPEG,100,stream);
    byte[] byteArray=stream.toByteArray();
    Intent intent = new Intent(getApplicationContext(), FrameActivity.class);
    intent.putExtra("Image", byteArray);
    startActivity(intent);

in FrameActivity.class在 FrameActivity.class

collageView = (CollageView) findViewById(R.id.btn_collage);
    byte[] byteArray = getIntent().getByteArrayExtra("Image");
    Bitmap bmp = BitmapFactory.decodeByteArray(byteArray, 0, byteArray.length);
    collageView.setImageBitmap(bmp);

Try passing id related to image through intent.putExtra(), and receive it through bundle on launched activity.尝试通过 intent.putExtra() 传递与图像相关的 id,并在启动的活动上通过捆绑接收它。

in Activity convert the image to ByteArray and append it to the intent as在 Activity 中将图像转换为 ByteArray 和 append 将其转换为意图

intent.putExtra("img",<ByteArray>);

then startActivity B.然后启动Activity B。

In Activity B在活动 B

Bitmap bm = BitmapFactory.decodeByteArray(getIntent().getByteArrayExtra("img"), 0, getIntent().getByteArrayExtra("img").length);

This way you can pass image between activity.这样您就可以在活动之间传递图像。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何将图像从一项活动传递到另一项活动 - How to pass image from one activity to another 如何从一个活动的列表视图传递数据并将其传递给另一活动? - How to pass the data from listview of one activity and pass it to another activity? 如何将一个活动的图像传递到另一活动? - How to pass image one activity to another activity? 如何将图像从一个活动传递到另一个活动 - How to pass image from one activity to another activity 如何将数据从一个活动传递到另一活动 - How to pass data from one activity to another activity 如何将数据从一个活动传递到 Android 中的另一个活动 - How to pass data from one activity to another activity in Android 我如何将数据从一个活动传递到另一活动 - how i pass data from one activity to another activity 如何将数据从活动传递到另一个活动,然后再传递到Android上的另一个活动? - How to pass data from an activity to another activity and then to another activity on Android? 将位图图像从一个活动传递到另一个活动 - pass a bitmap image from one activity to another Android如何将图像从图像视图传递到文本并将文本从文本从一个活动传递到另一个活动 - Android how to pass image from image view and text from text from one activity to another activity
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM