开发者

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.

How can I achieve this?

My MyGridView.java

开发者_如何学C
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.


In MyGridView: (someInteger is an integer that represents the index of the selected image

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

In 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


You pass parameters to an Activity in an Intent. If the image comes from a file, pass the path String, otherwise pass the 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:

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

Also go thro this link which wil also help u.
Here u can know how to convert bitmap to bytes


Once an item of Grid View is clicked, get the clicked item and pass it to next activity as an argument through PutExtra. In the next activity retrieve the image from extras and display it to user


I suppose you need use Intent class.

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

addinfo.putInt("imageid", someid);

intent.putExtras(addinfo);


Try passing id related to image through intent.putExtra(), and receive it through bundle on launched activity.


in Activity convert the image to ByteArray and append it to the intent as

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

then startActivity B.

In Activity B

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

This way you can pass image between activity.


This is my process: it's so good. Activity1:

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

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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜