Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
intent.setType("*/*");
// intent.setType(ContactsContract.CommonDataKinds.Phone.CONTENT_ITEM_TYPE);
startActivityForResult(intent, CHOOSE_FILE_RESULT_CODE);
I am working on an application that helps the user to share his/her any accessible item(file) in the device. Codes above help user to select file(s) in the device. It is perfect! But I also want to give an opportunity to users as sharing their contacts(full contact details) with others by using my app. I tried intent.putExtras() but it supported in Build.VERSION.SDK_INT >= 19 devices (my app's min SDK is 17). It seems contact pick action requires different kind of intent type rather than "*/*". So, how merge these types of intents together? What can I do to solve the problem?
p.s I added following line to the manifest:
<uses-permission android:name="android.permission.READ_CONTACTS"/>
Brandon Zamudio marked the question as possible duplicate of this question but I can explain why it is not. In original question Hardik Trivedi wanted to pick contacts using intent. But in my case I want to give some different options to user to select. So, my problem should not be same with the original question. I did not share some pieces of my codes, but it is different one. Thanks for attention.
Actually, I wrote almost same codes approximately 2 years ago. In two days I searched for these lines. Fortunately I found them in my second Hello World! application. :)
Intent takePicture = new Intent(MediaStore.ACTION_IMAGE_CAPTURE); // for Camera app
Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
intent.setType("*/*");
intent.addCategory(Intent.CATEGORY_OPENABLE); // not necessary, but recommended
Intent contacts = new Intent(Intent.ACTION_PICK, ContactsContract.Contacts.CONTENT_URI);
contacts.setType(ContactsContract.Contacts.CONTENT_TYPE);
Intent chooserIntent = createChooser(intent, "Select item");
chooserIntent.putExtra(Intent.EXTRA_INITIAL_INTENTS, new Intent[]{ takePicture, contacts });
startActivityForResult(chooserIntent, CHOOSE_FILE_RESULT_CODE);
Related
Background
In my Android App, users can share generated images to other apps. It's working nicely using the ACTION_SEND Intent.
Many users have asked why they can't share to Instagram stories directly.
Initially I thought Instagram doesn't support receiving Intents for stories (correct to some extent). I searched for it today, and according to this documentation, to share to Instagram Stories, a separate intent com.instagram.share.ADD_TO_STORY has to be used. I tried it, and it works fine.
The problem:
How do I keep both the options available?
I thought about it a lot, and came up with the following options:
1) Have two separate buttons. It will work, but it will look/feel bad.
2) Have my app accept ACTION_SEND intent, name it as Share to Instagram Story, and redirect the intent to the com.instagram.share.ADD_TO_STORY intent. In principle, make a proxy intent.
It will work, and look/feel great, but I don't know if its allowed (legal, etc) and can I disable the intent if the user doesn't have Instagram installed.
3) Add the 'com.instagram.share.ADD_TO_STORY' to the app chooser launched by ACTION_SEND. This would be ideal, but I don't know how to do it.
If you want to add multiple actions to Intent and create a chooser look at this example:
Intent viewIntent = new Intent(Intent.ACTION_VIEW);
Intent editIntent = new Intent(Intent.ACTION_EDIT);
viewIntent.setDataAndType(uri, type);
editIntent.setDataAndType(uri, type);
Intent chooserIntent = Intent.createChooser(editIntent, "Open in...");
chooserIntent.putExtra(Intent.EXTRA_INITIAL_INTENTS, new Intent[] { viewIntent });
startActivity(chooserIntent);
UPDATE: Here is good solution to your answer. How to make an intent with multiple actions
I tried the same approach as on Facebook's official documentation then tested on Huawai P9 Lite, Huawai P20 Lite and on Samsung S8 - it only worked on Samsung S8 for not known reason (to me). I gave up on it since, obviously, it's not working on most of the phones.
// Define image asset URI
Uri stickerAssetUri = Uri.parse("your-image-asset-uri-goes-here");
String sourceApplication = "com.my.app";
// Instantiate implicit intent with ADD_TO_STORY action,
// sticker asset, and background colors
Intent intent = new Intent("com.instagram.share.ADD_TO_STORY");
intent.putExtra("source_application", sourceApplication);
intent.setType(MEDIA_TYPE_JPEG);
intent.putExtra("interactive_asset_uri", stickerAssetUri);
intent.putExtra("top_background_color", "#33FF33");
intent.putExtra("bottom_background_color", "#FF00FF");
// Instantiate activity and verify it will resolve implicit intent
Activity activity = getActivity();
activity.grantUriPermission("com.instagram.android", stickerAssetUri, Intent.FLAG_GRANT_READ_URI_PERMISSION);
if (activity.getPackageManager().resolveActivity(intent, 0) != null) { activity.startActivityForResult(intent, 0);}
please help
how can i do preselection all for user in a native android gallery ?
here is a code
Intent galleryIntent = new Intent(Intent.ACTION_GET_CONTENT);
galleryIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
galleryIntent.putExtra(Intent.EXTRA_ALLOW_MULTIPLE, multiple);
galleryIntent.addCategory(Intent.CATEGORY_OPENABLE);
final Intent chooserIntent = Intent.createChooser(galleryIntent, "Pick an image");
activity.startActivityForResult(chooserIntent, IMAGE_PICKER_REQUEST);
its working fine, but initial stage 0 files are selected
thanks
There is no means of pre-selecting anything when using ACTION_GET_CONTENT, let alone pre-selecting everything.
Also note that ACTION_GET_CONTENT is not tied to a gallery app, and there is no single "native Android gallery" app across the ~10,000 Android device models.
In my application I allow to user to select multiple images to create pdf.
Issue is user is not able to select more than 1 images. I tried a lot but didn't get any perfect solution which can help me.
There are lot many lib available but i don't want to use it due to some reason.
I am looking for solution with native only.
Please help me.
Simple by putting extra:
int RESULT_IMAGE_MULTIPLE = 1;
Intent intent = new Intent();
intent.setType("image/*");
intent.putExtra(Intent.EXTRA_ALLOW_MULTIPLE, true);
intent.setAction(Intent.ACTION_GET_CONTENT);
startActivityForResult(Intent.createChooser(intent,"Select pictures"), RESULT_IMAGE_MULTIPLE);
I am trying to open gallery on Nexus 7 with Android 6.0. It does not have in built gallery, but it does have the Google Photos app.
I am using the following code to open the gallery :
Intent i = new intent(Intent.ACTION_PICK,android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
// Always show the chooser (if there are multiple options available)
startActivityForResult(Intent.createChooser(i, "Select Picture"), PICK_IMAGE_REQUEST);
The above code works well in all the versions below 6.0. And Please note that I am already using RUN TIME PERMISSIONS for accessing gallery and have given the permission to access gallery / or externa storage.
Now when the code is executed, I get a transparent screen with heading "Select Picture" and in the middle a text No Apps can perform this action.
Now what do I do to pick or choose image and use in my app.
Any help is appreciated.
Thanks
I am using the following code to open the gallery
That code has nothing to do with a "gallery". That code is requesting to pick a piece of content from a particular collection of content. There may be zero, one, or several activities on the device that offer to support that Intent structure.
The above code works well in all the versions below 6.0
Only on devices that happen to have one or more activities that satisfies that Intent structure.
Now what do I do to pick or choose image and use in my app.
Intent i = new intent(Intent.ACTION_GET_CONTENT).setType("image/*");
// use PackageManager to see if there is anything that supports this
// Intent structure, or just blindly make the following call and handle
// the ActivityNotFoundException
startActivityForResult(i, PICK_IMAGE_REQUEST);
I am using this code , so that only gallery opens and not any other option to pick image
There is nothing in your code that limits it to just a "gallery".
Not allowed to comment because I don't have enough points. But here's just a suggestion, how about if you just passed the intent directly? Like this:
startActivityForResult(i, PICK_IMAGE_REQUEST);
Use this line of code rather than what you have done
Intent i = new intent(Intent.ACTION_PICK,android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
startActivityForResult(i, PICK_IMAGE_REQUEST);
What I'm trying to do in my application is to let the user choose a picture from his phone's gallery(Don't want to get gallery images only but also allowing a user to choose their app of choice). The code I'm using is as follows:
Intent intent = new Intent();
intent.setType("image/*");
intent.setAction(Intent.ACTION_GET_CONTENT);
intent.putExtra(Intent.EXTRA_LOCAL_ONLY, true);
startActivityForResult(Intent.createChooser(intent, "Select Picture"), 1);
As per Intent.EXTRA_LOCAL_ONLY doesnt work
EXTRA_LOCAL_ONLY only tells the receiving app that it should return
only data that is present.
After adding intent.putExtra(Intent.EXTRA_LOCAL_ONLY, true); in above code,it hide google drive app and picasa app but still shows google photos(those photos are not in my device.)
Also I tried Android image picker for local files only but It hide all the apps having remote images excluding google photos.
Note : All the image paths are correct as I did Android Gallery on KitKat returns different Uri for Intent.ACTION_GET_CONTENT (thanks to #Paul Burke) but I don't want to pick internet/remote images.
So my question Is there any way to hide google photos app while pick an image from local device only. or Is google photos is part of Intent.EXTRA_LOCAL_ONLY
EXTRA_LOCAL_ONLY only tells the receiving app that it should return
only data that is present.
Google+ Photos stores both local and remote pictures and because of that is registered for that intent with that extra. However apparently it ignores wherever calling intent has EXTRA_LOCAL_ONLY set to true.
You could try removing G+ Photos from list manually (however this seems a bit hacky):
List<Intent> targets = new ArrayList<Intent>();
Intent intent = new Intent();
intent.setType("image/*");
intent.setAction(Intent.ACTION_GET_CONTENT);
intent.putExtra(Intent.EXTRA_LOCAL_ONLY, true);
List<ResolveInfo> candidates = getPackageManager().queryIntentActivities(intent, 0);
for (ResolveInfo candidate : candidates) {
String packageName = candidate.activityInfo.packageName;
if (!packageName.equals("com.google.android.apps.photos") && !packageName.equals("com.google.android.apps.plus") && !packageName.equals("com.android.documentsui")) {
Intent iWantThis = new Intent();
iWantThis.setType("image/*");
iWantThis.setAction(Intent.ACTION_GET_CONTENT);
iWantThis.putExtra(Intent.EXTRA_LOCAL_ONLY, true);
iWantThis.setPackage(packageName);
targets.add(iWantThis);
}
}
Intent chooser = Intent.createChooser(targets.remove(0), "Select Picture");
chooser.putExtra(Intent.EXTRA_INITIAL_INTENTS, targets.toArray(new Parcelable[targets.size()]));
startActivityForResult(chooser, 1);
Few words of explanation: targets.remove(0) will remove and return first Intent from targets list, so the chooser will consist only of one application. Then with Intent.EXTRA_INITIAL_INTENTS we add the rest.
The code snippet is modified version from this link.
Please remember to check all conditions like if there is at least one app available and so on.