Intent.ACTION_PICK behaves differently - android

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);
}

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.

Android : Why Intent.EXTRA_LOCAL_ONLY shows Google Photos

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.

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);

Picking images and videos on Android on 2.1 using ACTION_GET_CONTENT

I'm working on an app that needs to have the user select an image or video. On pre-2.1 devices, using ACTION_GET_CONTENT seems to work fine with multiple MIME types:
new Intent(Intent.ACTION_GET_CONTENT).setType("video/*, image/*")
However, on a Droid running 2.1, this gives a "There are no items in your collection". Using the same code with either "video/" or "image/" gives the desired result. Is there a way to get my 2.1 device to allow the user to select both types of content within a single Intent?
Putting the request into a function and then calling the function with onClick().
public void openGalleryImage(){
Intent intent = new Intent();
intent.setType("image/*");
intent.setAction(Intent.ACTION_GET_CONTENT);
startActivityForResult(Intent.createChooser(intent,"Select Image "),
SELECT_IMAGE);
}
public void openGalleryVideo(){
Intent intent = new Intent();
intent.setType("video/*");
intent.setAction(Intent.ACTION_GET_CONTENT);
startActivityForResult(Intent.createChooser(intent,"Select vVideo "),
SELECT_VIDEO);
}

Categories

Resources