Android - how to open image in gallery with delete option - android

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.

Related

After intent to show image not all functions appear

I'm going to develop a simple app to list .jpg files and after a click call an Intent.ACTION_VIEW, below the code:
File imageFile = new File(filename);
Intent i = new Intent();
i.setAction(android.content.Intent.ACTION_VIEW);
i.setDataAndType(Uri.fromFile(imageFile), "image/jpeg");
startActivity(i);
The intent works properly, in fact in app I receive the message to select an app to show the selected image. But I noticed that after the choice, for example with Google Photos, not all function are enabled. For example the share function is not visible, has someone already noticed this behavior?
Some other details; if I choose Google Photos I only have the "Info" and "Guide and Feedback" options. If I choose gallery app (I have a Samsung Ace 3) I have only "Info" and "Set as wallpaper" options.
Note: if I open the same image directly from Google Photos all functions are enabled...
The ACTION_VIEW intent calls another application to view the content that you present, in your case a JPEG image. If there is more than on application that can handle that Intent, Android lets the user choose an app to display the image. Once the application receives the Intent, it is up to that application to display the content how it sees fit.
Basically, you have no control over what functions are available with Intent.ACTION_VIEW. If you want more control, you can create your own Activity in which you can view the image and provide whatever functions you would like.

Displaying Images in Gallery and sharing them

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:

Tutorial / reference to launch ACTION_EDIT with image and return image

I'm writing an app that uses the camera. I want to lauch an intent to allow users to annotate the resulting image with lines and text, AND I would like to provide the user with a list of appropriate image editting apps they can use, but I'm coming across these problems:
1. Not all image editting apps appear in the list when I perform this code:
editIntent = new Intent();
editIntent.setAction(Intent.ACTION_EDIT);
Uri imageToEditUri = selectedPhotoLocation; // Uri of existing photo
String imageToEditMimeType = "image/*";
editIntent.setDataAndType(imageToEditUri, imageToEditMimeType);
startActivityForResult(Intent.createChooser(editIntent,"Edit Image"), IMPLICIT_EDIT_IMAGE);
Is there a way to get a list of apps that will respond to Intent.ACTION_EDIT?
2. PS Express is the only app I've found that returns the Uri of the editted image in the data.getDate() Uri returned to OnActivityResult(), with other apps the user is forced to save, remember the location, and reselect the editted image.
Is there a way to know what apps return the Uri of the image to OnActivityResult()
Not all image editting apps appear in the list when I perform this code
Just because an app implements image editing does not necessarily mean that it is set up to allow third parties to link to their image-editing activities.
Is there a way to get a list of apps that will respond to Intent.ACTION_EDIT?
If you mean that you want to do this programmatically at runtime, see Jedil's answer.
Is there a way to know what apps return the Uri of the image to OnActivityResult()
No, except for those applications that have documentation on how developers should integrate with them.
First question answer:
try queryIntentActivities
Intent editIntent = new Intent(android.content.Intent.ACTION_EDIT);
List<ResolveInfo> resInfo = getPackageManager().queryIntentActivities(editIntent, 0);

Open device gallary for view only

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.

modified android ACTION_VIEW intent

I execute the following code:
Uri uri =
Uri.withAppendedPath(MediaStore.Images.Media.EXTERNAL_CONTENT_URI,
imageIdStr); Intent intent = new
Intent();
intent.setAction(android.content.Intent.ACTION_VIEW);
intent.setData(uri);
activity.startActivity(intent);
which opens the image viewer activity but I don't want to see the navigation arrows as well as the items in the menu. I only want the zoom controls. Is it possible to have it like that ?
That depends on the application that's actually displaying the image. On Android 2.0 and older, it's probably the Gallery app. On many Android 2.1+ apps, it's probably CoolIris' gallery. If the user has one of the many gallery apps from the market installed, it's up to those apps. I highly doubt there's a standardized extra to control that.
If you're really concerned about what the image display looks like, then you should handle that yourself. If you're really just trying to display an image (and maybe add pinch-zoom support?), it's a reasonable scope.

Categories

Resources