2012-06-26 24 views
5

我敢肯定這是一個微不足道的問題,但我沒有找到答案。Java和Android:如何用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.

回答

1

我想要打開的圖像瀏覽器

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

但是,如何將多個圖像傳遞給查看器?

對不起,但沒有標準的Intent打開任何類型的多個文件。

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

+0

通過圖像瀏覽我的意思是默認打開與intent.action_view之一。關於多個文件,如果沒有多個文件,幻燈片如何工作? –

+0

@ LuisA.Florit:「通過圖像查看器我的意思是默認情況下打開的那個intent.action_view」 - 其中有數百個。 「如果沒有多個文件,幻燈片如何工作?「 - 圖片瀏覽者可能會或可能沒有」幻燈片顯示「功能 - 他們當然不是必需的。AFAIK,他們不會通過第三方應用程序使用的標準」Intent「動作字符串顯示」幻燈片「功能。 – CommonsWare

+0

通過定義「默認的」是指只有一個(當然是會發生變化的每個設置/裝置,但沒關係)。這就是爲什麼你不需要指定與我在原來的職位提供的代碼的瀏覽器,即工作反正,我所知道的所有觀衆管理多個文件。應該有辦法...... –

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

現在您可以讀取數組,然後逐個顯示。

+0

對不起,我聽不懂。你的意思是使用類似intent.setDataAndType(Uri.fromList(列表),「圖像/ JPG「);?(Uri.fromList不存在,我知道...) –

+0

你給的例子是隻顯示當時的一個圖像的示例代碼,我給它讀取一個文件夾,並挑選所有的(圖)需要顯示的文件在你的程序中,你需要讀取數組列表(命名列表)並獲取pa th +文件名來顯示它們。當你拖動你的android設備時,下一張照片需要被讀取。我正常地在Processing中編寫我的代碼,所以我無法提供正確的工作代碼。 – Dippo

+0

你的代碼是: Intent intent = new Intent(); intent.setAction(android.content.Intent.ACTION_VIEW); 文件file1 =新文件(列表[0] +列表[1]); intent.setDataAndType(Uri.fromFile(file1),「image/jpg」); startActivity(intent); 當您需要顯示下一張圖像時,需要將4添加到列表[0]和列表[1]中。 – Dippo

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這就是爲什麼你不能看其他的圖像。

如果你想看到的其他內容以外的圖像,然後更改「image/jpg」到「image/*