4

I have between one and three photos I'd like my app to display. I won't know until runtime exactly how many photos are downloaded from the Internet.

I can't figure out how to create an Intent to display the photos. Right now I'm caching them on the sdcard under a folder I create by doing something like (sans error checking):

final File externalDirectory = Environment.getExternalStorageDirectory();
final String folder = externalDirectory.getAbsolutePath() + "/Android/data/" + packageName + "/files/";

This was explained in the Android Developer Reference.

I can get one photo to display by doing the following:

final Uri uri = Uri.fromFile(file);
final Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setDataAndType(uri, "image/*");
Util.startActivity(this, intent);

Where file is the file of a saved photo.

If it helps, I could save the images to any location available to my app, however I would prefer to not have the photos show up listed with the user's other personal photos as that may be annoying.

The Image Viewer has a menu option "Slideshow", so it must know about multiple photos.

I could create my own Image Viewer, but that seems like extra work and beyond what I would I reasonably expect. Even if I did this, I would like the user to be able to install a 3rd party Image Viewer and get a better experience with pan, zoom, share, ...

I tried using the directory of the cached photo files to create the Uri, but the Image Viewer shows a black page. If I pass in the file, it shows just that one file and no others.

I know this must be possible because I can see use the Gallery app and show the photos if I manually select the folder. Everytime I research this issue, the comments say it's not possible to show multiple images.

I suspect there's some magic incantation, but what?

Michael Celey
  • 12,645
  • 6
  • 57
  • 62
Mitch
  • 1,716
  • 3
  • 25
  • 41

1 Answers1

4

I think your goal is out of your control. If the viewer app is designed to handle mutiple images or a directory, you may ask it to show as you want, but you are defined to the viewer's pattern.

I have installed a third-party image viewer called QuickPic. I just tested your code snippet and the system popped up a chooser dialog to let me select the app to show the images in the folder. If I select native gallery, what I see is just an empty folder, while the Quickpic works as I want.

PS: I tell my app the Uri of the folder this way:

intent.setDataAndType(Uri.fromFile(new File("//mnt/sdcard/test/")), MimeTypeMap.getSingleton().getMimeTypeFromExtension("png"));
T.Todua
  • 53,146
  • 19
  • 236
  • 237
Huang
  • 4,812
  • 3
  • 21
  • 20
  • Thanks. I'd mark this as answered, but I'd like to see if someone else can come up with something. I can see the Gallery app does what I want, so there must be a way, even if it isn't easy. – Mitch Mar 05 '12 at 03:54
  • @Mitch I suggest you review the source code of gallery so you'll get convinced if it is designed to do your task. I know it has a slideshow function, but this is not the same as showing image files in a directory. – Huang Mar 05 '12 at 04:40
  • I did review it to the best of my ability. I thought I saw a place where the mime type indicated a dir. I tried to use it, but I saw every photo on the phone, not just the ones for the directory I told it. You may very well be right that there is no way to do what I want, but It's important to let the user see the photos and not rely on them being savy enough to understand they need to install a 3rd party Image Viewer when the see a black screen. I'm confident they'll simply assume my app is broken. If I don't get an answer by the end of the week, I'll mark your answer as shockingly correct. – Mitch Mar 05 '12 at 16:25
  • 1
    Okay, still no answers, so I'll assume showing multiple images is either impossible or so poorly documented that no one knows. That is until someone can prove me wrong. – Mitch Mar 06 '12 at 18:05