Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

android can't get byte array from intent

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

like image 755
turtleboy Avatar asked Feb 23 '23 00:02

turtleboy


1 Answers

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

like image 145
devunwired Avatar answered Feb 25 '23 17:02

devunwired