2012-01-04 87 views
2

如何打開一個對話框,列出可打開給定文件夾的所有應用程序?打開文件夾 - 意圖選擇器

我嘗試下面的代碼

Intent intent = new Intent(Intent.ACTION_GET_CONTENT); 
intent.setData(Uri.fromFile(Environment.getExternalStorageDirectory())); 
startActivity(Intent.createChooser(intent, "Open Folder")); 

它說:「沒有應用程序可以執行此操作。」

我的設備具有默認的文件資源管理器和AndroidZip應用程序(這些應用程序可以打開文件夾)。

回答

4

它說「沒有應用程序可以執行此操作」。

這並不奇怪。你並沒有要求打開一個「文件夾」,因爲Intent系統中沒有「文件夾」這樣的東西。您正試圖找出哪些應用程序可以打開沒有文件擴展名且沒有MIME類型的路徑。您的設備上沒有安裝任何應用程序,其<intent-filter>在支持ACTION_GET_CONTENT的活動上沒有文件擴展名且沒有MIME類型。您可以嘗試ACTION_VIEW。不過,請記住,我估計90%以上的Android設備都沒有以這種方式處理「文件夾」的內容。

+0

謝謝你,CW!你真的是一個職業球員,並且對其他球員不僅僅是專業人士。 – herbertD 2012-04-24 08:28:35

+0

一個簡單的問題,我如何導航到一個文件夾?說該文件夾包含所有的MP3。 – 2014-01-27 18:51:56

+0

@Aashish:如果你看過我的回答,那麼在Intent系統中就沒有這樣的文件夾。 – CommonsWare 2014-01-27 18:52:57

2

試試這裏。適用於我。

public void openFolder() 
{ 
Intent intent = new Intent(Intent.ACTION_GET_CONTENT); 
Uri uri = Uri.parse(Environment.getExternalStorageDirectory().getPath() 
    + "/yourFolder/"); 
intent.setDataAndType(uri, "*/*"); 
startActivity(Intent.createChooser(intent, "Open folder")); 
} 

here

1

看到,因爲是目錄中的文件不似乎有一個標準的MIME類型,我編寫我的文件瀏覽器來過濾意圖沒有MIME類型。只需將數據設置到您的文件夾的Uri。

Intent intent = new Intent(Intent.ACTION_VIEW); 
Uri uri = Uri.parse("file:///sdcard/MyFolder"); 
intent.setData(uri); 
startActivity(intent); 

此意圖將啓動以下程序:

http://play.google.com/store/apps/details?id=com.organicsystemsllc.thumbdrive

我覺得這也能很好地打開其中一個文件或根據擴展文件夾。文件夾的URI的文件擴展名和MIME類型最終在下面的代碼片段中爲空。這仍然適用於匹配方案「文件」篩選但沒有特定MIME類型的活動,即Thumb Drive。請注意,當uri是目錄時,此方法不會找到過濾所有文件MIME類型的活動。

//Get file extension and mime type 
Uri selectedUri = Uri.fromFile(file.getAbsoluteFile()); 
String fileExtension = MimeTypeMap.getFileExtensionFromUrl(selectedUri.toString()); 
String mimeType = MimeTypeMap.getSingleton().getMimeTypeFromExtension(fileExtension); 

//Start Activity to view the selected file 
Intent intent = new Intent(Intent.ACTION_VIEW); 
intent.setDataAndType(selectedUri, mimeType); 
startActivity(Intent.createChooser(intent, "Open File...")); 
1

它的工作原理:

Uri selectedUri = Uri.parse(Environment.getExternalStorageDirectory() + "/myFolder/"); 
Intent intent = new Intent(Intent.ACTION_VIEW); 
intent.setDataAndType(selectedUri, "resource/folder"); 
startActivity(intent); 

有一個很好的作弄:)

+1

。它不會打開文件夾,而是會拋出活動未找到的異常。 – dira 2014-10-30 12:02:12

+1

很可能您沒有在您的設備上安裝資源管理器應用程序。請檢查它。 – 2014-11-22 00:31:54

+0

@Sa Qada可以用默認文件管理器打開嗎?默認文件管理器是否具有intent-filter ACTION_VIEW? – 2016-05-27 09:18:00

0

不幸的是,像@CommonsWare說,有犯規存在於股票的Android系統的 「資源管理器」 的標準清晰度(除非你安裝第三方「文件資源管理器」)..
這就是爲什麼"resource/folder" mime類型默認情況下不工作..

最大我找到了,是這樣的:https://stackoverflow.com/a/41614535/2377343