0

I have a class, ExpandableListAdapter that fires an intent that opens the camera

ImageView itemImage = (ImageView) convertView.findViewById(R.id.itemImage);
itemImage.setImageBitmap(childItem.Image);
itemImage.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        Camera camera = new Camera(context);
        // The method below fires the intent to the camera app
        camera.dispatchTakePictureIntent(childItem);
        }
    });

The childItem is an object from this class:

public class Item {
    public String Name;
    public Bitmap Image;
    public boolean IsChecked;
}

The camera takes a picture and stores it on the memory card.

I have overridden void onActivityResult(int requestCode, int resultCode, Intent data) in the activity that uses the adapter.

How do I get the item object (that I sent in the intent) back in the activity method:

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {}

EDIT: This is where the intent is fired:

public void dispatchTakePictureIntent(Item item) {
// ... I make a temp file with the name item.Name and fire the intent
Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(photoFile));
((Activity) context).startActivityForResult(takePictureIntent, REQUEST_TAKE_PHOTO);
Stefan
  • 188
  • 2
  • 19
  • 1
    Are you looking to get an Image back or have you passed the whole class object through :)? – SmiffyKmc Apr 30 '16 at 19:50
  • The whole class object would be the optimal solution. The idea is to receive back the class object (from which the user will take a picture), so that I can change that item's picture right away in the expandable list view. – Stefan Apr 30 '16 at 19:52
  • @Stefan you can find some ideas here [How do I pass data between activities on Android?](http://stackoverflow.com/questions/2091465/how-do-i-pass-data-between-activities-on-android) – Robert Apr 30 '16 at 22:29
  • @Stefan You never set the item to the intent. Your Item needs to implement Parcelable. – Robert May 01 '16 at 04:15
  • @Stefan other recommendation.. you can create and configure the Item object inside the onActivityResult method. Is jut a recommendation. – Robert May 01 '16 at 16:04

2 Answers2

0

I'm assuming you're calling the show camera method from your Activity. Therefore, simply keep a class variable in your activity and set the value before opening the camera.

Now, that value should be accessible on your onActivityResult method.

Ruchira Randana
  • 4,021
  • 1
  • 27
  • 24
  • The intent that calls the camera is fired within this method: camera.dispatchTakePictureIntent(childItem); Camera is class that simply just calls the intent to open the camera app. I pass the childItem as parameter, so that I can get the childItem.Name, for the image name. – Stefan Apr 30 '16 at 19:59
  • Is your ExpandableListAdapter declared in a different file other than your activity file? – Ruchira Randana Apr 30 '16 at 20:02
  • Yes, the adapter and the activity are different files. – Stefan Apr 30 '16 at 20:08
  • In that case, use a Singleton and write the value before opening the camera UI. Then on the "onActivityResult" method, you can get the value via the Singleton object. – Ruchira Randana Apr 30 '16 at 20:12
  • Yes, I could do that, but It's not exactly an elegant solution. :) I could also store a static variable in the Camera class: Item recentItem, and access it when I receive the result from the intent, but I was hoping for a better solution. Thank you anyway, I appreciate the idea. – Stefan Apr 30 '16 at 20:18
  • Yes Stefan. I agree with you. I thought you were stuck there without a clue! If you do find a better solution, please post it for us too! – Ruchira Randana Apr 30 '16 at 20:19
  • My bad, I didn't think of including my ideas in the description, because they are not exactly what I'm looking for. If I think of a better solution I will let you know. – Stefan Apr 30 '16 at 20:24
  • That's great Stefan. Thank you too! – Ruchira Randana Apr 30 '16 at 20:27
0

Did you try implements Parcelable interface??

This is my implementation of Parcelable interface:

public class Item implements Parcelable {

    int value;
    String otherValue;

    public Item (int value, String otherValue){
        this.value = value;
        this.otherValue = otherValue;
    }

    protected Item(Parcel in) {
        value = in.readInt();
        otherValue = in.readString();
    }

    public static final Creator<Item> CREATOR = new Creator<Item>() {
        @Override
        public Item createFromParcel(Parcel in) {
            return new Item(in);
        }

        @Override
        public Item[] newArray(int size) {
            return new Item[size];
        }
    };

    @Override
    public int describeContents() {
        return 0;
    }

    @Override
    public void writeToParcel(Parcel dest, int flags) {
        dest.writeInt(value);
        dest.writeString(otherValue);
    }
}

Sending the Item object:

public class MainActivity extends AppCompatActivity  {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }

    public void sendData(View view){
        Item item = new Item(10, "otherValue");
        Intent itemIntent = new Intent(MainActivity.this, ItemInfoActivity.class);
        itemIntent.putExtra("data", item);
        startActivity(itemIntent);
    }
}

I am using a Button component and the sendData is call in the onClick events.

Retrieve the Item object

public class ItemInfoActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_item_info);

        Item item = getIntent().getExtras().getParcelable("data");
        if(item == null)
            Toast.makeText(this, "NULL", Toast.LENGTH_LONG).show();
        else {
            Toast.makeText(this, "NOT NULL", Toast.LENGTH_LONG).show();
            Log.i("CCC", "Value: " + Integer.toString(item.value));
            Log.i("CCC", "OtherValue: " + item.otherValue);
        }
    }
}

In your case you can do the same using the activity for result but you need to use the putExtra to set insert the Item object inside the Intent and then asking for the extra value in Intent data parameter:

protected void onActivityResult(int requestCode, int resultCode, Intent data)

Enjoy it!!

Robert
  • 10,403
  • 14
  • 67
  • 117
  • The problem is that I fire an intent that calls the camera app. Then the user takes a picture, and when he closes the camera app, the data parameter in onActivityResult() is null. (I tried putting a simple string with putExtra()). I think when I use putExtra() that data is available for the camera app, but when the intent returns the data is lost. I need the data that I sent with the Intent to come back to me on onActivityResult() in the data parameter. – Stefan May 01 '16 at 10:06
  • @Stefan are you trying to retrieve the item object or the image path? – Robert May 01 '16 at 15:28
  • I need the Item object that I set in the intent, to come back in the onActivityResult() – Stefan May 01 '16 at 16:28