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!
Related
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().
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);
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.
I am adding some large serializable object (say data) to the Intent's putextra() method:
Intent intent = new Intent(currentScreen, newScreen.getClass());
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
Bundle bundle = new Bundle();
intent.putExtra("DATA", data);
intent.putExtras(bundle);
currentScreen.startActivity(intent);
newScreen taking much time to start and display.
Please let me know how to overcome the issue.
Thanks
Android_IT
Don't just assume the large extra data is causing your app to be slow. Profile it with Traceview and make sure. If it does indeed turn out to be the problem, my only suggestion is to store it in a static member before starting the new activity and then retrieve it from there. This way it will not be copied around and serialized/deserialized.
If NewScreen take too much time you are probably doing some time consuming task in the onCreate method of NewScreen.See what is happening there by printing some log to see whether onCreate of newScreen fired immediately or not
I am using two Intents for various actions in my activity and I also used onActivityResult to get a result. I want to know how to get to Intent result in onActivityResult? Can someone please provide sample code?
Thanks all.
The Intent result is the Intent passed as the third parameter to onActivityResult().