get result of intent without OnActivityResult - android

I need to get the path of the result image path from intent without using the onActivityResult.
Intent intent = new Intent();
intent.setType("image/*");
intent.setAction(Intent.ACTION_GET_CONTENT);
startActivityForResult(Intent.createChooser(intent, "Select Picture"), SELECT_PICTURE);
I need the path of the image to the next step in OnCreate which is after the this intent started. Is there any way to get this done without using OnActivityResult ??

I need the path of the image to the next step in OnCreate which is after the this intent started
That is not possible. startActivityForResult() does not even begin doing its work until after your current callback (onCreate(), apparently) returns.
Is there any way to get this done without using OnActivityResult ?
No.
I need the result image path to pass it to a new method and start a new intent.
Do that in onActivityResult().
this intent can be only created inside the onCreate due to my application structure
That seems unlikely. Even if for some bizarre reason it is true, create the base Intent in onCreate(), store it in a field of your activity, and use the Intent in onActivityResult().

Related

startActivityForResult - put Value to onActivityResult

I'm starting an Intent for choosing an Image from Gallery. My target is to put an Extra (an int value) to onActivityResult, but I can figure out how to do this.
The way I call the image chooser:
Intent photoPickerIntent = new Intent();
photoPickerIntent.setType("image/jpg");
photoPickerIntent.setAction(Intent.ACTION_GET_CONTENT);
Intent photoResultIntent = Intent.createChooser(photoPickerIntent, "Bild auswählen");
fragment.startActivityForResult(photoResultIntent, HorseFragment.PICK_IMAGE, bundle);
What I tried (with no success):
instantiating a new Bundle and give it to startActivityForResult() as third parameter
using photoResultIntent.putExtra...
How can I put a value to my target activity and retrieve it in onActivityResult? Can anyone explain it to me?
Thank you a lot for your time and your help!

Android Intent setSelector

Why when I use setSelector method it returns neither Camera activities nor Gallery Activities.I have the following code:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Intent camera= new Intent("android.media.action.IMAGE_CAPTURE");
Intent gallery = new Intent();
gallery.setAction(android.content.Intent.ACTION_VIEW);
gallery.setType("image/*");
gallery.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
camera.setSelector(gallery);
startActivity(camera);
}
According to Android's reference API "If the selector is set, it will be used when trying to find entities that can handle the Intent, instead of the main contents of the Intent."
I thought that I will get a dialog that opens the gallery instead of camera. But, instead of these I got a dialog that returns arbitrary applications/activities, such as Call Settings, Network Settings, SIM Toolkit, etc.
When I remove the camera.setSelector(gallery); method everything works like a charm, but when I use camera.setSelector(gallery); method it returns neither Camera activities nor Gallery Activities.
Could somebody explain me why I got a dialog with these arbitrary activities instead of appropriate?
I don't want to remove the camera.setSelector(gallery); because I am trying to understand how it works!
use this intent to open your gallery
Intent pickPhoto = new Intent(Intent.ACTION_PICK,
android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
then use startActivityForResult to get the selected photo from gallery. hope this helps
Replace this code :
Intent camera= new Intent("android.media.action.IMAGE_CAPTURE");
With this :
Intent camera= new Intent(Intent.ACTION_VIEW,android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);

Retrieving putExtra values from onActivityResult intent

I am trying to append certain data to an intent, before using StartActivityForResult on it.
When the intent returns in OnActivityForResult, I would like to access the data I appended in the intent. So I can correlate the data retrieved in the intent, with things like database entries, container ids, etc.
Unfortunately the intent that returns does not seem to be the same one I started. I tried comparing (==) the old and the new intent in a test case, and the result failed, and not surprisingly then the data I am trying append is not there. Is there any link back to the original intent?
Basic idea of what I've tried:
Code to StartActivityForResult in psuedo code:
Intent i = new Intent(Intent.ACTION_PICK, android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI)
i.putExtra([-Key-], [int]);
i.putExtra([-Key-], [int]);
....
getParentFragment().startActivityForResult(i, requestCode);
Pseudo Code for OnActivityResult
#Override
public void onActivityResult(int requestCode, int resultCode, Intent intent) {
....
switch(requestcode){
case RESULT_LOAD_IMAGE :
//These always evaluate to default. The intent returns with the picture,
//and I process it fine (with default values), but any extra data i try to append
//to the intent is lost.
int rowId = intent.getIntExtra([-Key-], [-def_value-]);
....
....
break;
default:
throw new RuntimeException();
}
}
When you launch an Activity using implicit Intent resolution, which is what you are doing when you do this:
Intent i = new Intent(Intent.ACTION_PICK, android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI)
....
getParentFragment().startActivityForResult(i, requestCode);
you don't have any guarantees what Activity will actually be chosen to perform the action. Because of this, there isn't any "contract" between your Activity and the Activity that will be used to perform the desired action. This is, unfortunately, one of the disavantages of using implicit Intent resolution. Because there is no contract between the 2 Activities, you can't be sure what you are going to get in the result that is returned to you in onActivityResult().
If you look at the documentation for ACTION_PICK, it at least indicates what "should" happen (if the selected Activity actually behaves the way the documentation reads):
Input: getData() is URI containing a directory of data
(vnd.android.cursor.dir/*) from which to pick an item.
Output: The URI of the item that was picked.
This indicates that you should provide a URI that contains a directory of data and that you will be returned an Intent containing the URI of the item that was picked. That's it. That's all you can expect to get. You can put lots of other "extras" in the Intent that you pass to the Activity with ACTION_PICK, but that Activity doesn't care about those extras and will just ignore them. The Activity that performs the ACTION_PICK will create a new Intent containing the URI of the selected item and pass that back to you. It doesn't pass your original Intent back. The "input Intent" and the "output Intent" are completely different and don't have anything to do with each other.
To solve your problem, I'd suggest that you create a unique integer requestCode and save your "extras" in a table or map in your Activity associated with that requestCode. Then you can launch the ACTION_PICK activity using the requestCode. In onActivityResult() you can use the requestCode argument that comes back to find your "extras" that you saved and you'll be able to associate the returned URI with them.
NOTE: One more thing: When you call startActivityForResult() your Activity will be paused and the launched Activity will run. Your Activity won't be resumed until onActivityResult() is called. This means that you will only ever be able to have one ACTION_PICK pending at any given time. For this reason you may not need a way to associate a specific PICK action with any given data.

Call another appllication from my application

I am creating an application where I need to call another application that is already installed in the device on button click.
I have done some research on it and I understand that I will need to call an intent for the same. What I dont understand is I do not have a class name for the application I want to call. For example, if I want to call the device's gallery from my application on button click, how do I do that?
Uri uri = Uri.parse("file:///sdcard/");
Intent intent = new Intent();
intent.setAction(Intent.ACTION_VIEW);
intent.setData(uri);
intent.setType("application/pdf");
startActivity(intent);
return true;
Thanks guys. I have tried this code but it said document could not be opened.
You will need to call Implicit Intents
From the documentation:
Implicit Intents have not specified a component; instead, they must
include enough information for the system to determine which of the
available components is best to run for that intent.
These intents can be triggered providing any action, type or category information
For example you want to open browser Activity and you don't know the Activity class name you will use something like this:
Intent in = new Intent(Intent.ACTION_VIEW, Uri.parse("http://www.google.com"));
startActivity(in);
Another example: you don't know the Gallery Activity class, you will call it using Implicit Intent like this:
Intent intent = new Intent();
intent.setType("image/*");
intent.setAction(Intent.ACTION_GET_CONTENT);
startActivityForResult(Intent.createChooser(intent,"Select Picture"), SELECT_PICTURE);
Here the call
Enjoy coding....
Intent res = new Intent();
String mPackage = "com.ReachOut";
String mClass = ".splash1";
res.setComponent(new ComponentName(mPackage,mPackage+mClass));
startActivity(res);
If the exact application is less important, and you just need something that will display your content, you can omit the component name entirely, too. Just set the action, data, and (optionally) type on the intent and let the OS do the work.
Intent intent = new Intent();
intent.setAction(Intent.ACTION_VIEW);
intent.setData(/*your data uri*/);
intent.setType(/*your data's MIME type*/);
startActivity(intent);
This intent will be handled by some app that has registered for intents with the view action and the appropriate MIME type. This is an implicit intent, like Adil mentioned.

Pick an image from the Gallery

I have seen a lot of posts about this, and it seems like the code below should work. I have created an SD Card image and added it to the emulator (and that works fine).
Intent intent = new Intent(Intent.ACTION_PICK);
intent.setType("image/*");
//intent.setAction(Intent.ACTION_GET_CONTENT);
startActivityForResult(intent, 1);
It does launch and allow selection of images, but when I click on an image, everything exits and the emulator returns to the home screen, not the back to my app. My onActivityResult is never called either.
What am I missing?
I found my issue. I was launching the gallery from a sub-activity and that sub activity Intent had the flag FLAG_ACTIVITY_NO_HISTORY which prevented the call back from going to that activity.
thanks.
Use the following intent :
Intent intent = new Intent(Intent.ACTION_GET_CONTENT, null);
intent.setType("image/*");
intent.putExtra("return-data", true);
startActivityForResult(intent, 1);

Categories

Resources