简体   繁体   English

Java和Android:如何用Intent打开几个文件?

[英]Java and Android: How to open several files with an Intent?

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. 我正在制作一个Android应用程序,我想从中打开显示多个图像的图像查看器。 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. L.

I want to open the image viewer 我想打开图像查看器

There is no "the image viewer" in Android. Android中没有“图像查看器”。 Devices and users may have many, many different apps that are capable of viewing image/jpeg files loaded from a local file. 设备和用户可能有许多不同的应用程序,它们能够查看从本地文件加载的image/jpeg文件。

But how do I pass several images to the viewer? 但是如何将几个图像传递给观众?

Sorry, but there is no standard Intent to open multiple files of any sort. 抱歉,没有标准的Intent可以打开任何类型的多个文件。

Also, please do not hardcode /mnt/sdcard/ in your app. 此外,请不要在您的应用程序中硬编码/mnt/sdcard/ Please use the proper methods on the Environment class to determine directories on external storage. 请使用Environment类上的正确方法来确定外部存储上的目录。

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. 现在您可以逐个读取数组并显示。

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. 您可以删除Environment.DIRECTORY_PICTURES如果您想要SD卡的父目录。

Since you are specifying a single file 20397a.jpg that's why you are unable to see other images. 由于您指定的是单个文件20397a.jpg ,这就是您无法看到其他图像的原因。

And if you want to see other contents other than images then change ' image/jpg ' to ' image/* ' 如果你想看到除图像以外的其他内容,那么将' image/jpg '更改为' image/* '

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM