Java and Android: how to open multiple files with intent?

I am sure this is a trivial question, but I did not find the answer.

I am creating an Android application from which I want to open an image viewer showing several images. I know how to do this with just 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);

It works great. But how to convey multiple images to the viewer?

Thank!! L.

+5
source share
3 answers

I want to open image viewer

There is no "image viewer" on Android. Devices and users can have many different applications that can view files image/jpegdownloaded from a local file.

But how to convey multiple images to the viewer?

, Intent .

, hardcode /mnt/sdcard/ . Environment.

+1

, . , , .

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();
      }
    }
  }

.

+1

.

:

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

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

/storage/emulated/0/Picture

Environment.DIRECTORY_PICTURES SD-.

20397a.jpg, .

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

+1
source

All Articles