2

I call startActivityForResult event from girdview adapter class. Gridview adapter class was created by myFragment class. intent extras always return null onActivityResult event. Where do you think the error? Thanks.

My gridview adapter class;

 public class MyAdapter extends ArrayAdapter<MyInfo> {

@Override
public View getView(int position, View convertView, ViewGroup parent) {

    View view = convertView;

    if (view == null) {

        LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        view = inflater.inflate(layoutResourceID, null);

    }

    ImageButton btnImage = (ImageButton) view.findViewById(R.id.btnImage);

        @Override
        public boolean onLongClick(View v) {

            Intent intentCamera = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);

            intentCamera.putExtra("Asd", "Asd");

            MyFragment.startActivityForResult(intentCamera, 1);

            return true;
        }
    });
  }

MyFragment class for onActivityResult event

public class MyFragment extends Fragment {

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

    super.onActivityResult(requestCode, resultCode, data);

    ImageButton btn = (ImageButton) getActivity().findViewById(R.id.btnImage);

    switch (requestCode) {
    case 1:

        Bitmap photo = (Bitmap) data.getExtras().get("data");

         //photo is OK

        Bundle bundle = data.getExtras();

        if (bundle.getString("Asd") != null) {

            //bundle.getString("Asd")  NULL                         }

        if (data.hasExtra("Asd")) {

            //data.hasExtra("Asd") false
        }


        break;

    default:
        break;
    }

}
}
lol
  • 41
  • 5
  • 2
    where are you returning the result? – SMR Sep 22 '14 at 11:35
  • For anyone trying to pass any value after capture just use a `public variable` or `public static variable` there is no other way ( maybe there is ? but I do it this way). – Naveen Niraula Dec 02 '18 at 06:28

4 Answers4

2

Your custom string "Asd" is not going to be returned by the Camera app - that is not how it works.

You can either expect to have a photo image returned back to you, or the image file.

If you are just asking about the "Asd", then that is your answer - you won't get it back. If you are asking why no picture is coming back, then read further on.


(left in as part of previous answer)

If you want to save the photo taken by the camera, you can follow these steps:

You need to pass correct data to the Camera in your Intent. As per the Android Developer docs for image capture Intent:

private static final int CAPTURE_IMAGE_ACTIVITY_REQUEST_CODE = 100;
private Uri fileUri;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    // create Intent to take a picture and return control to the calling application
    Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);

    fileUri = getOutputMediaFileUri(MEDIA_TYPE_IMAGE); // create a file to save the image
    intent.putExtra(MediaStore.EXTRA_OUTPUT, fileUri); // set the image file name

    // start the image capture Intent
    startActivityForResult(intent, CAPTURE_IMAGE_ACTIVITY_REQUEST_CODE);
}

... where method getOutputMediaFileUri() is a method you write to create the file where you want to save the image.


For more info on calling the Camera app, see these similar questions:

There you will see some more working examples.

Community
  • 1
  • 1
Richard Le Mesurier
  • 29,432
  • 22
  • 140
  • 255
  • 1
    I can get photo with the Bitmap photo = (Bitmap) data.getExtras().get("data"); code.There is no problem.But I can not get string extra parameter.Intent getstringextra return null – lol Sep 22 '14 at 12:10
  • @lol yes that is correct, so is it just the "Asd" that you want? You can't have that unfortunately... – Richard Le Mesurier Sep 22 '14 at 12:12
  • I want to pull image name to intent.Because I want to save as image for different name. – lol Sep 22 '14 at 12:17
  • @lol if you want to save the image, then you must call the app according to the example in my answer - note that you pass in the URI of a file that the image will get written to. The camera app should then save the image instead of returning the whole image back to you. – Richard Le Mesurier Sep 22 '14 at 12:24
0

You are calling android default camera application Each activity can accept a set of extras that are predefined and will also return the same. Hence such type of extras for camera intent cannot be found in OnActivityResult

Ex: intent.putExtra(MediaStore.EXTRA_OUTPUT, fileUri);

Suneel Prakash
  • 389
  • 5
  • 7
0

Replace this line:

MyFragment.startActivityForResult(intentCamera, 1);

With this one:

getApplicationBaseContext().startActivity(intentCamera);
grael
  • 657
  • 2
  • 11
  • 26
Maj Maj
  • 1
  • 2
-1
  Bundle bundle = data.getExtras();

you are pulling intent in a bundle without added bundle in intent. If you need a string from intent directly , call

 data.getStringExtra("Asd")
dharmendra
  • 7,835
  • 5
  • 38
  • 71