簡體   English   中英

Java和Android:如何用Intent打開幾個文件?

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

我確信這是一個微不足道的問題,但我沒有找到答案。

我正在制作一個Android應用程序,我想從中打開顯示多個圖像的圖像查看器。 我知道如何只使用一個圖像:

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

這非常有效。 但是如何將幾個圖像傳遞給觀眾?

謝謝!! L.

我想打開圖像查看器

Android中沒有“圖像查看器”。 設備和用戶可能有許多不同的應用程序,它們能夠查看從本地文件加載的image/jpeg文件。

但是如何將幾個圖像傳遞給觀眾?

抱歉,沒有標准的Intent可以打開任何類型的多個文件。

此外,請不要在您的應用程序中硬編碼/mnt/sdcard/ 請使用Environment類上的正確方法來確定外部存儲上的目錄。

您需要列出要在數組中查看的所有文件。 然后顯示其中一個數組,拖動時顯示下一個圖像。

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

現在您可以逐個讀取數組並顯示。

不要使用硬編碼路徑。

改變這一行:

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 ,這就是您無法看到其他圖像的原因。

如果你想看到除圖像以外的其他內容,那么將' image/jpg '更改為' image/* '

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM