This code successfully retrieves images from the (emulator) SDcard:
public void pickImage(View v) {
Intent intent = new Intent(Intent.ACTION_PICK, android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
intent.setType("image/*");
if (intent.resolveActivity(getPackageManager()) != null) {
startActivityForResult(intent, REQUEST_IMAGE_GET);
}
}
This returns a bunch of thumbnails titled Choose Picture. Is there any way I can intercept this and filter out certain images first?
Clicking any thumbnail image then runs the OnActivityResult which I can intercept, but then it is too late.
I am using Metadator-extractor and only need to display images containing certain tags, but do not know how to access each
thumbnail before it arrives on the screen.
Is there any way I can intercept this and filter out certain images first?
Not with ACTION_PICK. You are welcome to query MediaStore for available pictures and implement your own pick activity, in which you can filter out whatever you want.
Related
I use following code to open an image in the gallery:
public void onItemClicked(PictureItem item){
Intent intent = new Intent();
intent.setAction(Intent.ACTION_VIEW);
Uri imageUri = FileProvider.getUriForFile(getApplicationContext(), "myapp.fileprovider", new File(item.uri.getPath()));
intent.setDataAndType(imageUri, "image/*");
intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
startActivity(intent);
}
This shows my photo in the gallery, but in a 'read-only' mode. I want to be able to delete the image from there, just as if I opened it directly in the gallery.
Which action do I have to use for that? I do not want to use pick, just normal view with the option to delete. I tried ACTION_EDIT but it's not supported (and not quite the right choice neither ...).
I use following code to open an image in the gallery
First, there are hundreds, if not thousands, of "gallery" apps available for Android.
Second, your code simply asks to view an image (with a broken Intent due to your wildcard MIME type). There is no requirement for the app that responds to be a "gallery" app.
I want to be able to delete the image from there, just as if I opened it directly in the gallery.
Then implement that yourself, in your own app, and get rid of the ACTION_VIEW Intent.
Which action do I have to use for that?
There is no Intent action that says "please display this image, but only if you are a gallery app, and, oh, by the way, you must offer a delete option", which appears to be what you want.
in my app i would like to have the user choose an image from the gallery and retrieve the result (thumbnail + full image uri). i would also like the user to be able to choose which gallery app to open (i.e. default gallery app or some other gallery app).
initially, according to this guide by google i copied and pasted this code:
public void selectImage() {
Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
intent.setType("image/*");
if (intent.resolveActivity(getPackageManager()) != null) {
startActivityForResult(intent, REQUEST_IMAGE_GET);
}
}
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == REQUEST_IMAGE_GET && resultCode == RESULT_OK) {
Bitmap thumbnail = data.getParcelable("data");
Uri fullPhotoUri = data.getData();
// Do work with photo saved at fullPhotoUri
...
}
}
while it does get me the thumbnail and the full Uri in onActivityResult(...), the problem is that it does not open a chooser for the user to select which gallery app to use and instead it opens this thing (see images below), which i assume is a default "image-chooser" thing where you could select another app via a menu.
i feel it's silly that the user would have to open this default "image chooser" first and once they are already in an "image chooser", select the gallery app that they actually want to use (sure, the user could just choose the image from this thing, but i want to give them a convenient choice).
so i changed my code to this and it does indeed display a proper chooser for the user and he can go straight to hes favorite gallery app:
Intent intent = new Intent(Intent.ACTION_PICK,
MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
intent.setType("image/*");
if (intent.resolveActivity(getActivity().getPackageManager()) != null)
{
startActivityForResult(intent, Utils.REQUEST_CODE_OPEN_GALLERY);
}
however now i have a new problem: in onActivityResult(...) the line data.getParcelable("data"); returns null. in other words, i don't get back a thumbnail.
i also tried
Bundle extras = data.getExtras();
Bitmap thumbnail = (Bitmap) extras.get("data");
but "extras" is null.
is it possible to have a proper "app chooser" AND get back a thumbnail?
however now i have a new problem: in onActivityResult(...) the line data.getParcelable("data"); returns null. in other words, i don't get back a thumbnail.
You do not get that from ACTION_GET_CONTENT, either. You can tell this by reading the documentation for ACTION_GET_CONTENT. It is certainly possible that there are some buggy ACTION_GET_CONTENT activities that happen to return a thumbnail Bitmap in a "data" extra, but you should not assume that any user will have such a buggy ACTION_GET_CONTENT activity, let alone choose it. Besides, ACTION_GET_CONTENT is not limited to images; what would a "thumbnail" be of application/json or text/csv be?
Likewise, if you read the documentation for ACTION_PICK, you will see that there is no discussion of a thumbnail.
is it possible to have a proper "app chooser" AND get back a thumbnail?
No, insofar as you do not get a thumbnail back from anything except ACTION_IMAGE_CAPTURE, and that is for taking photos.
Hi I have used below code to pick video from gallery
Intent intent = new Intent();
intent.setType("video/*");
intent.setAction(Intent.ACTION_GET_CONTENT);
intent.addCategory(Intent.CATEGORY_OPENABLE);
intent = Intent.createChooser(intent, context.getString(R.string.choose_video));
startActivity(activity, fragment, intent, REQUEST_VIDEO_FROM_GALLERY);
private static void startActivity(Activity activity, Fragment fragment, Intent intent, int requestCode) {
if (fragment != null) {
fragment.startActivityForResult(intent, requestCode);
} else {
activity.startActivityForResult(intent, requestCode);
}
}
But its not only showing the local video's like in WhatsApp Messenger, when opening gallery. I'm in need to restrict to show only local video while opening gallery like in WhatsApp Messenger. Please suggest me some idea.
I have also tried EXTRA_LOCAL_ONLY only like in below mentioned link. It shows local videos, but it also shows other documents.
Android : Why Intent.EXTRA_LOCAL_ONLY shows Google Photos
Can any one please suggest me an idea to show the local video when picking video from gallery using intent? Thanks in advance.
There is no way to request local-only content using ACTION_GET_CONTENT. EXTRA_LOCAL_ONLY is for ACTION_OPEN_DOCUMENT, and even there it is a request, not a demand.
EXTRA_LOCAL_ONLY is now documented for use with ACTION_GET_CONTENT. However, there is no guarantee that all ACTION_GET_CONTENT implementations will honor it.
You are welcome to query the MediaStore for videos directly and render your own selection UI. AFAIK, MediaStore should only contain videos that are available on the device itself.
My application is capable of capturing images and saving them (in the public Pictures directory, retrieved by Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES).
Now, I want to be able to display those images in the system's gallery application. In order to achieve that, I create an Intent like this (filePath is a String containing the image's path):
Intent viewIntent = new Intent();
viewIntent.setAction(Intent.ACTION_VIEW);
Uri uri = Uri.fromFile(new File(filePath));
viewIntent.setDataAndType(uri, "image/jpeg");
startActivity(Intent.createChooser(viewIntent, null));
Displaying an image this way works perfectly fine, but the gallery's Share functionality doesn't seem to work:
When I press the Share-button, I get to choose the app I want to share to; but once I select it, nothing happens, as if the target application does not receive any data.
Is there any explanation why this behaviour occurs and how it can be fixed?
Edit: I used Intent Intercept to capture the Intent created by the Gallery/Photos app:
I find this code here to open the gallery from my own btn:
btnGallery.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
Intent intent = new Intent();
intent.setType("image/*");
intent.setAction(Intent.ACTION_GET_CONTENT);
startActivityForResult(Intent.createChooser(intent, "Select Picture"), 0);
}
});
It works, but when I click on a photo it exit the viewer.
I guess it because the "createChooser".
How can I change it to only view the photos and not to choose them?
As far as I know this is impossible.
If you had access to a full class name of an activity to start the Gallery app you could call it as you usually do it Activity.startActivity(Context context, Class clazz). But Gallery classes is an internal API which you have no direct access to.
Well, Gallery app is accessible via throwing an appropriate Intent like the one in your sample code. By setting action name intent.setAction(Intent.ACTION_GET_CONTENT) you request the behavior you've got (browse all images, select one, get back to the caller activity with a uri of a selected image). There is also another possible action Intent.ACTION_VIEW, and if set with a uri of an image it causes Gallery to show that image for you. But that's all we can request from Gallery (there are no other predefined actions to suit your needs - just to browse images).
So, a way out is to create your own custom image browser activity.