android can't get byte array from intent

15,229

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

Share:
15,229
turtleboy
Author by

turtleboy

linkedin

Updated on June 04, 2022

Comments

  • turtleboy
    turtleboy almost 2 years

    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

  • turtleboy
    turtleboy over 12 years
    hi amended the code to what you've suggested but it still throwing NPE on the arr array.
  • turtleboy
    turtleboy over 12 years
    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