FAILED BINDER TRANSACTION while passing Bitmap from one activity to another

匆匆过客 提交于 2019-11-29 08:45:14

You can use a global class with a static bitmap object in it, something like this:

public class Global { static Bitmap img; }

Before stating the activity by intent, assign your bitmap to this Global class attribute:

Global.img = your_bitmap_img;

After starting your activity, you can get back your bitmap by:

bitmap_in_new_activity = Global.img;

I know global variables are too dangerous for debugging but this technique helps us to transfer large data from one activity to another.The binder transaction buffer has a limited fixed size, currently 1Mb regardless of your device capabilities or your app.

Bitmap bmp = BitmapFactory.decodeResource(getResources(), R.drawable.f1);
    ByteArrayOutputStream stream = new ByteArrayOutputStream();
    bmp.compress(Bitmap.CompressFormat.PNG, 100, stream);
    byte[] food = stream.toByteArray();
    Intent intent = new Intent(MainActivity.this,NoBoringActionBarActivity.class);
    intent.putExtras(bundle);
    intent.putExtra("picture", food);
    startActivity(intent);  

Sending Activity

Bundle extras = getIntent().getExtras();


    byte[] food = extras.getByteArray("picture");
    Bitmap fo = BitmapFactory.decodeByteArray(food, 0, food.length);
    mHeaderLogo = (ImageView) findViewById(R.id.header_logo);
    //ImageView image = (ImageView) findViewById(R.id.header_logo);
    mHeaderLogo.setImageBitmap(fo);   

Receiving Activity

Don't forget to place your image in drawable.

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