开发者

android can't get byte array from intent

开发者 https://www.devze.com 2023-04-04 03:30 出处:网络
i\'m trying to send a byte[] from one activity to another. in the recieving activity the byte[] seems to be null after getting the intent extras. any ideas?

i'm trying to send a byte[] from one activity to another. in the recieving activity the byte[] seems to be null after getting the intent extras. any ideas?

thanks.

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

.

[updated] i've changed the key values so the are not the same data/bytrArr, also the intent now just passes a Bundle


The value of the keys is not your problem. You're not retrieving the data in the same way that you are putting it in.

In the first section of code, you are putting a byte[] inside a Bundle, and then putting that Bundle into the Intent extras. This means that the EXTRA at the key "data" is a Bundle, not a byte[]. You have no need to insert the extras in this fashion. Simply do intent.putExtra("byteArr", touchView.data) to insert the byte[] as an Extra.

Doing this, you will be able to retrieve your byte[] back with getIntent().getByteArrayExtra("byteArr") in the second section of code.

Finally, just as a side note, if you DID have multiple extras you wanted to apply with one call, you could put each one into a Bundle and then call Intent.putExtras(bundle) to have all the data from the Bundle placed individually into the Intent. But this is not the same as adding that Bundle as an extra itself.

HTH


Dont give the same key name to both the extras. Give a different name.

Just call intent.putExtra(bundle); for putting the bundle in the intent.


Replace

intent.putExtra("data",bundle );

with

intent.putExtras(bundle );

0

精彩评论

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