4

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

3 Answers 3

8

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

Sign up to request clarification or add additional context in comments.

2 Comments

hi amended the code to what you've suggested but it still throwing NPE on the arr array.
hi, just tested it again and you are correct. it something else to do with my code not setting the array with data the first time round when you first boot the app up. thanks
0

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.

Comments

0

Replace

intent.putExtra("data",bundle );

with

intent.putExtras(bundle );

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.