Is there any way to create an intent that will open that gallery and will show a specific image in there? I'm not asking about saving the image to the memory but I just want a proper and easy way to show the image (not in an ImageView) in such a way that zooming will be available.
Asked
Active
Viewed 38 times
1 Answers
1
You can use Intent
and ACTION_VIEW
to achieve that:
Intent intent = new Intent();
intent.setAction(android.content.Intent.ACTION_VIEW);
Uri uri = Uri.parse("file://" + "YOUR_FILE's PATH");
//Or you can use
Uri uri = Uri.fromFile(new File("YOUR_FILE's PATH"));
intent.setDataAndType(uri,"image/*");
startActivity(intent);
If you're targeting or the device is API level 24+, refer to this answer to see how to get the proper Intent and avoid FileUriExposedException

Jack
- 5,354
- 2
- 29
- 54
-
Is it possible to do so without saving the image? – Daniel Reyhanian Apr 23 '19 at 21:26
-
I think it has to be saved, at least to a temporary place. I don't think there's a way for gallery apps to resolve something from the memory directly. – Jack Apr 23 '19 at 21:29
-
By the way, is "file://" really needed? – Daniel Reyhanian Apr 23 '19 at 21:32
-
If your file path already contains that then no, otherwise you have to include that so the system knows what kind of `Uri` it is. You can also use the `Uri.fromFile()` to get the `Uri` instance if you have a `File` instance – Jack Apr 23 '19 at 21:33
-
What's the file's path like? Are there any error logs? – Jack Apr 23 '19 at 21:39
-
I get `Android.OS.FileUriExposedException: file:///storage/emulated/0/Screenshots/pic1.jpg exposed beyond app through Intent.getData()` – Daniel Reyhanian Apr 23 '19 at 21:39
-
Oh I see, I think you're probably targeting API 24+, see this [post](https://stackoverflow.com/a/38858040/8170714) – Jack Apr 23 '19 at 21:41
-
So I am supposed to check the user's API level? – Daniel Reyhanian Apr 23 '19 at 21:46
-
Yes, I should rephrase it, if the device is above API 24, you have to do that – Jack Apr 23 '19 at 21:47
-
And isn't there any other way to zoom in an imageview? – Daniel Reyhanian Apr 23 '19 at 21:48
-
There is probably a way to zoom in, but you asked for gallery intent in this question, that's why I answered that – Jack Apr 23 '19 at 21:50
-
Got you. By the way, I need READ_EXTERNAL_STORAGE permissions? – Daniel Reyhanian Apr 23 '19 at 21:52
-
I don't think so since you are not reading the file yourself, you are just providing it to a content provider. BUT if you want to save the file to a storage before this, you would need something like `WRITE_EXTERNAL_STORAGE` – Jack Apr 23 '19 at 21:53
-
Wait.. I thought that I need the file's path, which means that I already saved it in the storage. – Daniel Reyhanian Apr 23 '19 at 21:57
-
Yeah I know, if the file is already saved, you don't need any permission, but if you want to save it to storage, then you need the permission – Jack Apr 23 '19 at 21:58