I have an intent chooser that allows me to pick image from gallery or camera like this:
Intent galleryIntent = new Intent(Intent.ACTION_GET_CONTENT,null);
galleryIntent.setType("image/*");
galleryIntent.addCategory(Intent.CATEGORY_OPENABLE);
Intent cameraIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
Intent chooser = new Intent(Intent.ACTION_CHOOSER);
chooser.putExtra(Intent.EXTRA_INTENT, galleryIntent);
chooser.putExtra(Intent.EXTRA_TITLE, "title");
Intent[] intentArray = {cameraIntent};
chooser.putExtra(Intent.EXTRA_INITIAL_INTENTS, intentArray);
startActivityForResult(chooser,REQUEST_CODE);
I want my onActivityResult
method to be like this:
protected void onActivityResult(int requestCode, int resultCode, Intent data)
{
if(Condition == picture_coming_from_gallery)
{
//my code here
}
else if(condition == picture_coming_from_camera)
{
//another code here
}
}
What is the condition that allows me to know which source my image is coming from?
Updated:
Now it's working and here is the solution:
protected void onActivityResult(int requestCode, int resultCode, Intent data)
{
if (requestCode == REQUEST_CODE && resultCode == Activity.RESULT_OK)
{
if(data.getData()!=null)
{
try
{
if (bitmap != null)
{
bitmap.recycle();
}
InputStream stream = getContentResolver().openInputStream(data.getData());
bitmap = BitmapFactory.decodeStream(stream);
stream.close();
imageView.setImageBitmap(bitmap);
}
catch (FileNotFoundException e)
{
e.printStackTrace();
}
catch (IOException e)
{
e.printStackTrace();
}
}
else
{
bitmap=(Bitmap) data.getExtras().get("data");
imageView.setImageBitmap(bitmap);
}
super.onActivityResult(requestCode, resultCode, data);
}
}
Thank you for you help!
Although the current piece of code is a neat way of presenting options to choose from, I found that it was severely difficult to manage. At least in my use case it was. I need to store images taken off the Camera to process further in the Aviary SDK (if the user so chooses).
To that end, I would like to propose a workaround.
This does not address your question per se. But offers an alternative considering you need to know where the Image is coming from (Camera / Gallery).
This is the result (I still maintain that you code gives a much better selection set, but again, not the simplest thing in your use case or in mine either):
Now you can process the result based on the source of the selection:
UPDATED:
Found it!! There is an answer here on SO that addresses exactly what you need. It is still a * workaround of sorts*. In the sense that it does not rely on different
requestCodes
. But works nonetheless.Strange I missed it when I was stuck with this. :-(
NOTE: I am not posting the code here and am linking to it instead. All props go to the original author. :-)
You can distinguish by using REQUEST_CODE