2012-01-11 73 views
2

嗨我正在開發一個應用程序,我需要列出能夠打開文件的所有類型的應用程序。如果文件是一個圖像,我通常會這樣做,以顯示所有能夠查看圖像的應用程序。可以查看未知文件的Android列表應用程序

Intent intent = new Intent(); 
intent.setAction(android.content.Intent.ACTION_VIEW); 
intent.setDataAndType(Uri.fromFile(file), "image/*"); 
context.startActivity(intent); 

但是說,我有一個未知的文件,我怎麼能列出能夠查看文件,以便用戶可以選擇相應的應用程序在我的應用程序打開該文件的所有應用程序。

如果可能的話,我還需要在arraylist中列出的標題,以便我可以在listview中列出它們。

謝謝你的任何幫助,得到的能夠查看文件的應用程序列表

========================== =========

編輯

好的東西也更容易我怎樣才能從上面的意圖應用到一個ArrayList我只是做圖像/音頻*/*等,並添加他們都列表,然後在列表視圖中列出,這將解決我的問題

+0

你如何確定應用程序是否可以處理文件?如果您不是自己編寫所有這些應用程序,則必須能夠篩選您爲未知文件創建的意圖。這些應用程序在調用startActivity(intentWithUnknownFile)時如何知道過濾的意圖; – robertly 2012-01-11 18:56:26

回答

0

好吧好吧,我想通了我的問題了什麼我一直在尋找的是

Intent audioIntent = new Intent(android.content.Intent.ACTION_VIEW); 
    audioIntent.setDataAndType(Uri.fromFile(Opener.file), "audio/*"); 
    List<ResolveInfo> audio = packageManager.queryIntentActivities(audioIntent, 0); 
    for (ResolveInfo info : audio){ 
     String label = info.loadLabel(packageManager).toString(); 
     Drawable icon = info.loadIcon(packageManager); 
     String packageName = info.activityInfo.packageName; 
     String name = info.activityInfo.name; 
     iconlabel.add(a.new HolderObject(label, icon, audioIntent, "audio/*", packageName, name)); 
    } 

但在上面的代碼中主要的是queryIntentActivities方法竟然是解決我的問題,讓我將這些應用程序添加到列表

0

據我所知,android中沒有將文件鏈接到程序中的API,如在Windows中。你最好的選擇恐怕是建立一個已知的文件類型和程序/ Android應用程序鏈接到他們的數據庫。

+0

當我點擊一個未知文件時,出現一個「開啓者」,並且有三個選項卡,第三個選項卡列出了郵件,瀏覽器,文本查看器等應用程序,所有能夠打開未知文件基本上試圖複製該文件 – user577732 2012-01-11 19:30:51

0

這可能會幫助您列出能夠打開特定文件的應用程序。

// extension : The file extension (.pdf, .docx) 

String extension = filePath.substring(filePath.lastIndexOf(".") + 1); 
     String mimeType = MimeTypeMap.getSingleton().getMimeTypeFromExtension(extension.toLowerCase()); 

     if (null == mimeType) { 
      // Need to set the mimetype for files whose mimetype is not understood by android MimeTypeMap. 
      mimeType = ""; 
     } 
     File file = new File(filePath); 
     Uri data = Uri.fromFile(file); 
     Intent intent = new Intent(); 
     intent.setAction(Intent.ACTION_VIEW); 
     intent.setDataAndType(data, mimeType); 
     context.startActivity(Intent.createChooser(intent, "Complete action using")); 
相關問題