繁体   English   中英

android无法从意图中获取字节数组

[英]android can't get byte array from intent

我正在尝试从一个活动向另一个活动发送一个byte []。 在接收活动中,在获得意图附加功能后,byte []似乎为null。 有任何想法吗?

谢谢。

Button save = (Button)findViewById(R.id.save);
         save.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View arg0) {

                touchView.isSaved = true;
                Bundle bundle = new Bundle();
                bundle.putByteArray("byteArr", touchView.data);
                Intent intent = new Intent(mContext, SavePic.class);

                intent.putExtra(bundle );



                startActivity(intent);


            }}) ;

 public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.savepic);


        final EditText edittext = (EditText) findViewById(R.id.edittext);
        edittext.setText("");

        edittext.setOnKeyListener(new OnKeyListener() {
            public boolean onKey(View v, int keyCode, KeyEvent event) {
                // If the event is a key-down event on the "enter" button
                if ((event.getAction() == KeyEvent.ACTION_DOWN) &&
                    (keyCode == KeyEvent.KEYCODE_ENTER)) {
                  // Perform action on key press

                    Bundle extras = getIntent().getExtras();
                    byte [] arr = extras.getByteArray("byteArr");
                    if(arr != null){
                        Log.e("xxxxxx", "********* arr not null");
                    }else{
                        Log.e("xxxxxx", "********* arr is null");
                    }
                      final Bitmap mCBitmap2 = BitmapFactory.decodeByteArray(arr, 0, arr.length);

[更新]我已经改变了键值,所以不是相同的数据/ bytrArr,现在的意图只是通过一个Bundle

键的值不是你的问题。 您没有以与放入数据相同的方式检索数据。

在代码的第一部分中,您将一个byte []放在Bundle ,然后将该Bundle放入Intent extras中。 这意味着键“data”处的EXTRA是Bundle,而不是byte []。 您无需以这种方式插入附加内容。 只需将intent.putExtra("byteArr", touchView.data)作为Extra插入byte []即可。

这样做,您将能够在代码的第二部分中使用getIntent().getByteArrayExtra("byteArr")检索您的byte []。

最后,正如旁注所示,如果您有多个额外内容,您希望应用一个调用,您可以将每个附加到一个Bundle中,然后调用Intent.putExtras(bundle)将Bundle中的所有数据单独放入意图。 但这与将Bundle添加为额外功能并不相同。

HTH

不要为这两个额外提供相同的密钥名称。 给出一个不同的名字。

只需调用intent.putExtra(bundle); 将捆绑包放入意图中。

更换

intent.putExtra(“data”,bundle);

intent.putExtras(bundle);

暂无
暂无

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

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