I am looking for a light easy way, to pick an image from a gallery, and pass to another intent/activity the image selected.
This is what I have but I know it is wrong, I was just looking for some quick insight.
I know this opens up an image to be selected, but once it's selected, nothing happens, it does not load my intent. I am aware, from my belief once the image is selected it is just not dong anything and is returning true or w.e. when chosen and not providing anything, to which I ask if I need some more methods to be called that I haven't included? thank you.
Code:
item.getTitle().equals("Upload")) {
// TODO might want to pass parameter of what fragment is loaded.
// Switch to upload activity to allow for uploading of images.
Intent uploadIntent = new Intent(this, UploadActivity.class);
uploadIntent.setType("image/*");
uploadIntent.setAction(Intent.ACTION_GET_CONTENT);
startActivityForResult(
Intent.createChooser(uploadIntent, "Select Picture"),
PICK_IMAGE);
//startActivity(uploadIntent);
}
I'm not sure I fully understand your requirements but if you're ok with launching the default system image gallery, then the following post may help you:
How to pick an image from gallery (SD Card) for my app?
The general idea is to request a new Activity handle an Intent requesting a result (startActivityForResult()). If an Activity is setup to receive the intent (ACTION_GET_CONTENT) then it will launch and handle your request. Once the request is complete, the Activity will return a result which your Activity will recieve in it's method
onActivityResult(int requestCode, int resultCode, Intent imageReturnedIntent);
Related
I am in a fragment which is a feedback form. When the user clicks on the button it takes the data from the form and passes it to an e-mail program that gets open after the user chooses it from the list after startActivity(intent).
My problem is, I am not sure if there is a way to get feedback to know when the 3rd party mailer is done or if the launcher is cancelled.
Also in my example, if you click the submit and then click on the gmail and then click send in gmail and close it out, it brings you back to the app with all the data still on the form.
I would like to switch fragments to a thank you fragment. I did experiment and was happy that if you click on a different icon int the bottom nav bar and go back to the feedback fragment that all the data is cleared out.
Thank you and all help would be appreciative.
Shawn Mulligan
P.S. There is no code as I don't feel it is needed, I just need to know what direction to go into and any code snippetts if available to do the next step. change fragments after intent.
public void sendFeedbackMessage(String subject, String message) {
Intent messageIntent = new Intent(android.content.Intent.ACTION_SENDTO);
messageIntent.putExtra(android.content.Intent.EXTRA_SUBJECT, subject);
messageIntent.setType("plain/text");
messageIntent.putExtra(android.content.Intent.EXTRA_TEXT, message);
messageIntent.setData(Uri.parse("mailto:foo#gmail.com"));
startActivity(messageIntent);
}
startActivityForResult(messageIntent,1000);
}
#Override
public void onActivityResult(int requestCode, int resultCode, Intent data){
super.onActivityResult(requestCode, resultCode, data);
if (resultCode == RESULT_OK){
if (requestCode == 1000){
Toast.makeText(getContext().getApplicationContext(),"You sent mail",Toast.LENGTH_SHORT).show();
}
}else{
Toast.makeText(getContext().getApplicationContext(),"Mail Not Sent",Toast.LENGTH_SHORT).show();
}
}
So I looked into startActivityForResult and onActivityResult and changed my code to the above. However it is doing nothing but Mail Not Sent. I did find out that gmail for one does not support this. I am totally fine with this, but shouldn't the chooser itself return something so that way the app would know if an app was chosen or the cancel or back button was used?
I'm trying to open a pdf file from my app , on my device currently installed 3 3rd party apps to open pdf file. when the system asks me on witch of them to open the pdf, how can i know if the user selects one and presses "ok" or "decline"? my actions defined by if he accepts or declines
This is in my class :
String path="...../file.pdf";
Intent intent=new Intent(Intent.ACTION_VIEW);
intent.setDataAndType(Uri.fromFile(new File(path)), "application/pdf");
startActivityForResult(intent, 225);
And then in the activity :
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
// Check which request we're responding to
if (requestCode == 225) {
// Make sure the request was successful if (resultCode == RESULT_OK)
} else {
}
}
screen shot for example
You can find all of the apps on the device that can service your Intent using the following code:
List<ResolveInfo> pkgAppsList = context.getPackageManager().queryIntentActivities( mainIntent, 0);
You can use the information from this to query the packages and learn their display name and icons (SO has plenty of answers how to do that).
You can create your own Dialog from within your app that looks identical to the system dialog. In this way you'll be able to track hits and pass the intent to the selected package directly.
Good luck!
how can i know if the user selects one and presses "ok" or "decline"?
You can't. ACTION_VIEW that is used to open a pdf in an external app does not give back any information. You can use startActivityForResult() but it will not have any effect because a result will not be set. Also see ACTION_VIEW documentation.
You should redesign your concept/logic so that it does not depend on this information.
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.
im trying to take a picture inside an android app, and im trying to use the android devloper tutorial:
http://developer.android.com/training/camera/photobasics.html
they bring the following code:
private void dispatchTakePictureIntent(int actionCode) {
Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(takePictureIntent, actionCode);
}
i can't understand what is this action code and what it should be for taking pictures
thanks!
Per Getting a Result from an Activity, the second parameter to startActivityForResult is used to distinguish between multiple different requests (say, if you got results from both the camera and the gallery you'd want to know where the result is from).
That same actionCode is then returned as the requestCode in onActivityResult:
protected void onActivityResult(int requestCode, int resultCode, Intent data)
The point is that it doesn't matter exactly what the number is (0, 1, 100, 5439, whatever), only that it is unique within your Activity. Therefore if you are only calling startActivityForResult in one place for one result, any number will do (as there is nothing to conflict with)
Intents are designed to allow your application to interact with others. In this case, your application will bring up the camera app, and the result will be sent back into your app.
A great place to get started with understanding this is, is the Android training "Interacting with Other Apps".
I'd like to set up something to launch the built-in contact picker, and let a user choose an email, phone number, etc, then get said address/phone number back in my activity.
Am I correct in assuming that this isn't possible with android? It seems like you have to at least build a dialog yourself to choose in the case where a contact has multiple phone numbers/emails.
Even before that though, there doesn't seem to be a way to choose both phone numbers and emails simultaneously.
Is something like https://github.com/codinguser/android_contact_picker or rolling your own UI the only way to go?
Its perfectly possible and straight forward with android. You don't need to create any dialog box or anything for it. Just lauch the built in contact picker Activity via an Intent.
It you have to launch the intent from some event handler
Intent contactPickerIntent = new Intent(Intent.ACTION_PICK, Contacts.CONTENT_URI);
startActivityForResult(contactPickerIntent, CONTACT_PICKER_RESULT);
You should also have to implement
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
}
to receive URI of the contact user picked.
Thousands of tutorials are available on INTERNET on how to do it. Google is your friend.
Here is one http://mobile.tutsplus.com/tutorials/android/android-essentials-using-the-contact-picker/