Android : Why Intent.EXTRA_LOCAL_ONLY shows Google Photos - android

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.

Related

Preselected 'all' by default in android.Intent gallery with multiple selection

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.

Suggest several apps in playstore to user (Similar to Share dialog)

My app forwards users to a different app to perform a specific action (e.g. ACTION_SHARE, except that the apps that I forward users to do not implement an intent filter) Since they don't implement intent filters, I have a list of package names that support the action.
This part is working fine, like this:
for (String knownApp : knownApps) {
Intent intent = pm.getLaunchIntentForPackage(knownApp);
if (intent != null) {
ResolveInfo resolveInfo = pm.resolveActivity(intent, 0);
intentList.add(new LabeledIntent(intent, knownApp, resolveInfo.loadLabel(pm), resolveInfo.icon));
}
}
LabeledIntent[] extraIntents = intentList.toArray(new LabeledIntent[intentList.size()]);
Intent openInChooser = Intent.createChooser(actionIntent, getString(R.string.perform_action_with));
openInChooser.putExtra(Intent.EXTRA_INITIAL_INTENTS, extraIntents);
startActivity(openInChooser);
If the user has none of the apps installed, I want to give the user a choice of apps to download to fulfill the action.
Currently that looks like this.
As you can see it's lacking both icon and name. For regular apps I use an intent chooser which needs LabledIntent, but on one hand, I can't get the name and icon from the playstore unless I scrape them (which is not allowed by google, besides LabledIntent requires a resourceId as the Icon, which I can't get for downloaded files.), on the other the intent chooser won't seem to display the intent unless the package name of the intent and LabeledIntent match. This does not work for URIs which I'm using to access the Play Store in the first place.
Now I'm looking for ideas on how to get the following code to display both the correct name and app icon, as well as forward to the correct page on the play store.
protected void showPlayStoreOptions(List<String> knownApps) {
Intent chooserIntent = new Intent();
Intent showIntent = Intent.createChooser(chooserIntent, "You need one of these Apps on Google Play..."); //googles brand guidelines state that "on Google Play" has to be used
List<Intent> list = new ArrayList<>();
for (String knownApp : knownApps) {
Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse("https://play.google.com/store/apps/details?id=" + knownApp)); //normally you would try an uri with market:// first, catch the exception if no playstore is installed and then use this, but the intent chooser seems to automatically forward correctly.
list.add(intent);
//list.add(new LabeledIntent(intent, "https://play.google.com/store/apps/details?id=" + knownApp, "test name", R.drawable.icon_info));
//list.add(new LabeledIntent(intent, ""+Uri.parse("https://play.google.com/store/apps/details?id=" + knownApp), "test name", R.drawable.icon_info));
}
showIntent.putExtra(Intent.EXTRA_INITIAL_INTENTS, list.toArray(new Intent[list.size()]));
startActivity(showIntent);
}
So to sum up my questions.
How can I get a resource Id from a downloaded image file, or how can I use the downloaded image file with a LabledIntent.
(Extending LabledIntent does not work due to issues with parceling (and those methods are package private))
How can I display a LabledIntent in a choose intent with an URI?
I realize it's probably easier to write my own chooser, but I want to wrangle this into the default android system.

Intent.ACTION_PICK behaves differently

I am using following code to pick an image from the gallery. When I test it with Samsung Galaxy S4, it directly goes to Gallery that is what I want actually.
BUT, when I test my code on LG Optimus II device, it shows a dialog gives an option to choose either Gallery or Picture. In other words, it adds one more layer which I do not want.
Both devices have KitKat 4.4.2 operating system.
public static void showFileChooser(Activity activity) {
Intent intent = new Intent();
intent.setType("image/*");
intent.setAction(Intent.ACTION_PICK);
activity.startActivityForResult(Intent.createChooser(intent, "Select Picture"), 1);
}
when I test my code on LG Optimus II device, it shows a dialog gives an option to choose either Gallery or Picture
That is because there are two activities on that device that support ACTION_PICK of image/* files. There can be zero to N such activities, depending on what apps are on the device. This will include both pre-installed apps and apps that the user installed themselves. These will range from local file managers to general cloud providers (e.g., Dropbox) to image-specific apps (e.g., Instagram).
In other words, it adds one more layer which I do not want.
Then do not use ACTION_PICK. You are delegating to a third-party app; it is up to the user, not you, what third-party app the user wishes to use.
if you want Gallery then
Intent pickImageIntent = new Intent(Intent.ACTION_PICK,
android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
please use for api 19 or above
if (Build.VERSION.SDK_INT < 19) {
Intent intent = new Intent();
intent.setType("image/jpeg");
intent.setAction(Intent.ACTION_GET_CONTENT);
startActivityForResult(Intent.createChooser(intent,
"Select image to promote"),
GALLERY_INTENT_CALLED);
} else {
Intent intent = new Intent(Intent.ACTION_OPEN_DOCUMENT);
intent.addCategory(Intent.CATEGORY_OPENABLE);
intent.setType("image/jpeg");
startActivityForResult(intent,
GALLERY_KITKAT_INTENT_CALLED);
}

How to pick multiple files from Android's gallery?

i need to open gallery and pick 1-n images OR 1-n videos (2 different intent) from Android's gallery, like the ACTION_PICK intent. How can I achieve that? There are some cool library on GitHub for multiple pick or custom gallery?
If you use API 18 or higher, you can add to your intent.putExtra like so:
Intent intent = new Intent();
intent.setType("image/*,video/*"); //For choosing both images and/or videos
intent.putExtra(Intent.EXTRA_ALLOW_MULTIPLE, true); //This should allow multiple selection
intent.setAction(Intent.ACTION_GET_CONTENT);
startActivityForResult(Intent.createChooser(intent,"Select Picture"), 1);
There is also a GitHub project to build your own GridView with multi selection
Read here
To pick multiple files from gallery you need to simply modify your intent. For example:
Intent i = new Intent();
i.setType("image/*");
i.putExtra(Intent.EXTRA_ALLOW_MULTIPLE, true);
i.setAction(Intent.ACTION_GET_CONTENT);
startActivityForResult(
Intent.createChooser(intent,"Select"), 1);
Please note the i.putExtra(Intent.EXTRA_ALLOW_MULTIPLE, true);

Pick Image From Android Device's Gallery App

In my android application, I implemented a feature to pick an image from Gallery. For that earlier I was doing this-
Intent pickImageIntent = new Intent(Intent.ACTION_GET_CONTENT);
pickImageIntent.setType("image/*");
startActivityForResult(pickImageIntent, GALLERY_REQUEST_CODE);
By doing this, a dialog with all available image source application like Dropbox including Native Gallery app were displayed and it asks to choose one. Then I changed to
Intent pickImageIntent = new Intent(Intent.ACTION_PICK,
android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
pickImageIntent.setType("image/*");
startActivityForResult(pickImageIntent, GALLERY_REQUEST_CODE);
By doing this most of applications were not diplayed but still some like Picasa are diplayed. I want to pick an image from Device's native gallery app only or we can say from either device's internal or external memory not from any third party application.
If any one has solution for this please help me with that.
To pick image from Gallery only
Intent pickImageIntent = new Intent(Intent.ACTION_PICK);
pickImageIntent.setType("image/*");
startActivityForResult(pickImageIntent, GALLERY_REQUEST_CODE);

Categories

Resources