【问题标题】:FAILED BINDER TRANSACTION while passing Bitmap from one activity to another将位图从一个活动传递到另一个活动时失败的 BINDER TRANSACTION
【发布时间】:2014-08-08 17:58:12
【问题描述】:

我想将图像作为位图从一个活动传递到另一个活动。我想知道是否可以这样做。

发送活动

Intent intent = new Intent(getApplicationContext(), BitmapActivity.class);
                Bundle b = new Bundle();
                b.putParcelable("BITMAP", bitmap);
                intent.putExtras(b);
                startActivity(intent);

接收活动

Bundle bb = this.getIntent().getExtras();
    b = bb.getParcelable("BITMAP");

但是我得到了!!! BINDER 交易失败!!!错误

【问题讨论】:

  • bitmap 位图对象吗?
  • 是的。我将图像存储在位图对象中。
  • 你不应该那样做。你最好将图像的路径传递给下一个活动。即使您压缩为字节然后传递它仍然需要更多时间。

标签: android android-intent android-bitmap android-bundle


【解决方案1】:

您可以使用其中包含静态位图对象的全局类,如下所示:

public class Global { static Bitmap img; }

在按意图说明活动之前,将您的位图分配给这个全局类属性:

Global.img = your_bitmap_img;

开始活动后,您可以通过以下方式取回位图:

bitmap_in_new_activity = Global.img;

我知道全局变量对于调试来说太危险了,但是这种技术可以帮助我们将大数据从一个活动传输到另一个活动。活页夹事务缓冲区具有有限的固定大小,无论您的设备功能或应用程序如何,目前都是 1Mb。

【讨论】:

  • 这种方法肯定会对我有所帮助,我正在通过捆绑传输许多照片,但无法进行下一个活动。我花了一段时间才知道一个包裹可以携带多少。 Thanx Balaji Dubey
【解决方案2】:
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);  

发送活动

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);   

接收活动

别忘了把你的图片放在drawable中。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-03-05
    • 1970-01-01
    • 1970-01-01
    • 2014-09-17
    相关资源
    最近更新 更多