6

I'm sure this is a trivial question, but I failed to find an answer.

I'm making an Android app from which I want to open the image viewer showing several images. I know how to do this with only one image:

    Intent intent = new Intent();  
    intent.setAction(android.content.Intent.ACTION_VIEW);
    File file1 = new File("/mnt/sdcard/photos/20397a.jpg");
    intent.setDataAndType(Uri.fromFile(file1), "image/jpg");
    startActivity(intent);

This works perfectly. But how do I pass several images to the viewer?

Thanks!! L.

Luis A. Florit
  • 2,169
  • 1
  • 33
  • 58

3 Answers3

1

I want to open the image viewer

There is no "the image viewer" in Android. Devices and users may have many, many different apps that are capable of viewing image/jpeg files loaded from a local file.

But how do I pass several images to the viewer?

Sorry, but there is no standard Intent to open multiple files of any sort.

Also, please do not hardcode /mnt/sdcard/ in your app. Please use the proper methods on the Environment class to determine directories on external storage.

CommonsWare
  • 986,068
  • 189
  • 2,389
  • 2,491
  • By image viewer I meant the one that is opened by default with intent.action_view. About the multiple files, how does a slideshow works if not with multiple files? – Luis A. Florit Jun 26 '12 at 16:56
  • @LuisA.Florit: "By image viewer I meant the one that is opened by default with intent.action_view" -- of which there are hundreds. "how does a slideshow works if not with multiple files?" -- image viewers may or may not have a "slideshow" capability -- they are certainly not required to. AFAIK, they do not expose a "slideshow" capability via a standard `Intent` action string for use by third party apps. – CommonsWare Jun 26 '12 at 17:03
  • By definition "the default one" means only one (that of course vary for each setup/device, but that's ok). That's why you don't need to specify the viewer with the code I provided in the original post, that works. Anyway, all viewers that I know of manage multiple files. There should be a way... – Luis A. Florit Jun 26 '12 at 17:11
1

You need to list all files you want to view in a array. Then you display one of the array and when you drag, you show the next image.

ArrayList list;

private DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); // Ansi date format

list = new ArrayList();  
  String path = "c:/temp/";  
  File dir = new File(path);   
  for (String dirListing : dir.list()) {
    if ((dirListing.endsWith(".jpg")) ||
      (dirListing.endsWith(".png")) || 
      (dirListing.endsWith(".gif"))) {
      try { // write all file-info to a arraylist
        File f = new File(path+dirListing);      
        list.add(f.getCanonicalPath()); 
        list.add(f.getName());
        list.add(String.valueOf(f.length()));
        String lastModified = dateFormat.format(new Date(f.lastModified()));
        list.add(lastModified);
      }
      catch (IOException e) {
        e.printStackTrace();
      }
      catch (IndexOutOfBoundsException e) {
        e.printStackTrace();
      }
    }
  }

Now you can read the array and display then one by one.

Dippo
  • 184
  • 2
  • 11
  • Sorry, I didn't understand. You mean to use something like intent.setDataAndType(Uri.fromList(list), "image/jpg"); ? (Uri.fromList does not exist, I know...) – Luis A. Florit Jun 26 '12 at 17:02
  • The example you gave is for only showing one image at the time. The example code i gave, it reads a folder and picks all (image) files it needs to be displayed. In your program you need to read the arraylist (named list) and get the path + filename to display them. When you drag on you android device, the next photo needs to be read. I normaly write my code in Processing, so i can't supply a correct working code. – Dippo Jun 26 '12 at 17:10
  • your code would be : Intent intent = new Intent(); intent.setAction(android.content.Intent.ACTION_VIEW); File file1 = new File(list[0]+list[1]); intent.setDataAndType(Uri.fromFile(file1), "image/jpg"); startActivity(intent); When you need to display the next image, you need to add 4 to list[0] and list[1]. – Dippo Jun 26 '12 at 17:13
  • Yes, I understood your code. What I don't understand is what is missing, the "intent..." part. You build a list of files, but how do you show them in the viewer? Which is the command to show a list of files, or to play a set of songs? (My true code already gives a list of files, so this is not a problem). – Luis A. Florit Jun 26 '12 at 17:14
  • So, do you mean just to reuse my original code each time I drag? (I don't see the difference between your's and mine). Wouldn't that open a new instance of the viewer? Sorry, as you realized, this is my very first code for android... :) – Luis A. Florit Jun 26 '12 at 17:17
  • It's very hard to display a list of files in Android. In the example i gave, the list of files are in a arraylist. And from this arraylist i work from. To make something like a file explorer or folder explorer, is a different question. – Dippo Jun 26 '12 at 17:22
  • Aha, a small hint. I use Processing because i can make code that runs on my PC/linux/mac, when i am happy, i convert it to Android code (also in Processing). The problem however, is that Android works a little bit different. For example, in Android you don't have a mouse. But other than that, it is much easier to understand than Android Java. – Dippo Jun 26 '12 at 17:30
  • I tested it. If I understood correctly your suggestion, that opens one image viewer for each image. That is too slow, and there's no way to going back to the previous image, or going fast between images. This is not what I need. (What I did was just a loop using the last 3 lines of my original code. But maybe I did not understand you, sorry...) – Luis A. Florit Jun 26 '12 at 17:34
  • I am using Eclipse (I already have everything working fine for linux). – Luis A. Florit Jun 26 '12 at 17:36
  • It looks like this answer is much harder than I thought... Is there any 'standard' image viewer in Android where we simply do a " ..." ? – Luis A. Florit Jun 26 '12 at 18:28
1

Do not use Hard Coded Paths.

Change this line :

File file1 = new File("/mnt/sdcard/photos/20397a.jpg");

to

File sdDir = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES);

It will locate to

/storage/emulated/0/Picture

You can remove Environment.DIRECTORY_PICTURES If you want the parent directory of your sdcard.

Since you are specifying a single file 20397a.jpg that's why you are unable to see other images.

And if you want to see other contents other than images then change 'image/jpg' to 'image/*'

Sourav Roy
  • 323
  • 4
  • 12