简体   繁体   中英

Pass Bitmap from one Activity to another

I tried to convert a TextView into Bitmap and I tried to pass it to another Activity on a single Button click.

My code is:

Activity sending the Bitmap :

 save.setOnClickListener(new View.OnClickListener() {

@Override
public void onClick(View v) {
        View view0 = txt1.getRootView();  //txt1 is a TextView
        view0.setDrawingCacheEnabled(true);
        view0.buildDrawingCache();
        Bitmap bmp0 = Bitmap.createBitmap(view0.getDrawingCache());
        Intent in = new Intent(Meme_make.this,S_meme.class);
        in.putExtra("bm0" , bmp0);
        startActivity(in);
 }

Activity Receiving the Bitmap :

protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    setContentView(R.layout.sm);
    main1 = (ImageView) findViewById(R.id.imageView1);
    Bitmap bitmap = getIntent().getParcelableExtra("bm0");
    main1.setImageBitmap(bitmap);
}

But when I click on the button , the activity is automatically closing and taking me back to the Main launcher Activity.

How to do this without any issues ?

You have

in.putExtra("bm0" , bmp0);

and

Bitmap bitmap = getIntent().getParcelableExtra("bmp0");

In particular bm0 vs bmp0, you may just be missing a p. But in general I would also cast your retrieved bitmap to a bitmap

Bitmap bitmap = (Bitmap)getIntent().getParcelableExtra("bmp0");

Try this code snippet. Send bitmap

Intent intent = new Intent(this, ActivityName.class);
intent.putExtra("Bitmap", bitmap);

Retrieve Bitmap

Bitmap bitmap = (Bitmap) getIntent().getParcelableExtra("Bitmap");

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM