3

I'm trying to send an image path from one activity to another. I'm catching the intent in onResume, but the string path is always null. I don't know what I'm doing wrong. Hopefully you guys can help me with this problem.

Here's my activity where I grab the image path and send an intent.

private Intent testImage = new Intent(this, MyActivity.class);

@Override
public void onCreate(Bundle savedInstanceState)
{
    super.onCreate(savedInstanceState);
    setContentView(R.layout.card_create_layout);
    testImage = new Intent(this, MyActivity.class);
}


private void grabImage()
{
    Intent imageGetter = new Intent(Intent.ACTION_PICK, MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
    startActivityForResult(imageGetter, RESULT_LOAD_IMAGE);
}

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data)
{
    super.onActivityResult(requestCode, resultCode, data);
    if(requestCode == RESULT_LOAD_IMAGE && resultCode == RESULT_OK && null != data)
    {
        Uri selectedImage = data.getData();
        String[] filePathColumn = {MediaStore.Images.Media.DATA};//Array size of 1, and we put in a string
        Cursor cursor = getContentResolver().query(selectedImage, filePathColumn, null, null, null);
        cursor.moveToFirst();
        int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
        user_image_path = cursor.getString(columnIndex);//here we have our image path.
        cursor.close();
        ImageView imageView = (ImageView) findViewById(R.id.imageView);
        imageView.setImageBitmap(BitmapFactory.decodeFile(user_image_path));
    }

    testImage.putExtra("the_image", user_image_path);

}

@Override
public void onClick(View v)
{
    switch (v.getId())
    {
        case R.id.theCreateButton:
            grabImage();
            break;
        case R.id.theDesButton:
            startActivity(sendInformation);
    }
}

@Override
public void onBackPressed()
{
    super.onBackPressed();
    MyActivity.checkCard();
    setResult(Activity.RESULT_OK, getIntent());
    finish();
}

Now in my other activity, when I grab the image and press back

@Override
public void onResume()
{
    super.onResume();
    String ImagePath = getIntent().getStringExtra("the_image");
    if(ImagePath == null)
    {
        Toast.makeText(this,"hello everyone",Toast.LENGTH_LONG).show();
    }
}

It keeps on showing the toast message "hello everyone", which means ImagePath is continuously null. How do I fix this?

Jared Rummler
  • 35,743
  • 18
  • 127
  • 142
TheQ
  • 1,770
  • 6
  • 33
  • 60

2 Answers2

1

Pass mechanism between activities is available in three ways :

  • via DI(Dependency Injection)
  • via Bundle mechanism
  • via Singletone class which play a role a bridge or data holder between activities.

To avoid duplicating answer - please search any way(i recommend easiest - via Bundle) in stackoverflow.

Quick guide :

  1. You put your string into bundle via intent.putExtra(String name, String value) in Activity A
  2. Start this intent with startActivity(intent);
  3. In B activity read value view getIntent().getStringExtra(String name) in OnCreate method.

name value is need the same in activity A and B. This is a key.

Community
  • 1
  • 1
Sergey Shustikov
  • 13,329
  • 9
  • 57
  • 110
  • But, why won't an intent work this way?, I mean using Bundle vs intent won't matter too much, – TheQ Aug 17 '15 at 21:12
  • @TheQ because you try read a value in `OnResume`. You should do that in `OnCreate` or `OnNewIntent` if activity was created before. – Sergey Shustikov Aug 17 '15 at 21:13
  • so if I use Bundle instead of intent, it should read information from another activity? – TheQ Aug 17 '15 at 21:14
  • @TheQ you need understand how it works firstly. 1. You put your string into bundle via `intent.putExtra(String name, String value)` 2. Start this intent with `startActivity(intent)`; 3. In second activity read value view `getIntent().getStringExtra(String name)` in `OnCreate` method. **name** value is need the same in activity A and B. This is a *key*. – Sergey Shustikov Aug 17 '15 at 21:17
  • Yeah I think I figured it out, I didn't put startActivity(intent) in onBackpressed, after that it worked. Thanks bud. – TheQ Aug 17 '15 at 21:21
  • @TheQ your pleasure. However, i recommend to you read attentively passing mechanism. Processing in `OnResume` method is not a good way, it may be produce unexpected result. – Sergey Shustikov Aug 17 '15 at 21:23
  • So what would you recommend I pass to, if I want to press back into a previous activity A, and send information from Activity B -> Activity A when pressing back. – TheQ Aug 17 '15 at 21:28
1

I don't see a

 startActivity(testImage);

If you aren't using that intent to start the activity, then there is no extra called 'the_image' and the getStringExtra function will effectively return a null.

@Override
public void onBackPressed() {

Intent intent = new Intent();
intent.putExtra("the_image", user_image_path);
setResult(RESULT_OK, intent);
super.onBackPressed();
} 

The above is how to correctly do it, if you have started an activity for result.

ginsengtang
  • 721
  • 1
  • 6
  • 16
  • the first activity, I grab an image, and then I press the back button to go to my next activity where I grab the image. I don't want to use startActivity(testImage) because I don't want to startActivity as soon as I grab the image. I want the user to grab the image, then when all is said and done, they click "back" and that image they picked should be sent to the next activity to be used. – TheQ Aug 17 '15 at 21:16
  • Yeah I think I figured it out, I didn't put startActivity(intent) in onBackpressed, after that it worked. Thanks bud. – TheQ Aug 17 '15 at 21:21