2012-08-30 137 views
3

我正在製作一個Android應用程序,它將一些下載的pdf文件存儲在設備的SD卡內。 一切工作正常,但現在我想添加一個功能,只彈出默認的Android文件/文件夾瀏覽器,顯示我的應用程序存儲所有PDF(其子目錄)的目錄,以便用戶可以看到他的文檔存儲在哪裏並可以輕鬆瀏覽它們。從我的Android活動打開文件選取器

我已經經歷了很多其他的SO問題和論壇帖子,但似乎這隻能做音樂/圖像/聯繫人/等。基本上是那些具有「專用瀏覽系統」但不包含一般文件瀏覽的文件類型。

我實際使用此代碼:

File file = new File("/sdcard/MySorgenia/Documenti/"); 
Intent intent = new Intent(); 
intent.setAction(android.content.Intent.ACTION_VIEW); 
Uri data = Uri.fromFile(file); 
String type = "*/*"; 
intent.setDataAndType(data, type); 
startActivity(intent); 

但是,這會告訴我一個「選擇應用程序來完成你的行動」對話框中有許多應用,如「音樂」,「圖片」等,但不通用目的之一。

謝謝!

回答

9

因爲在Android中沒有任何本地應用程序,它可以作爲一個文件資源管理器使用,並響應Intent type "*/*"

實現自己File-Explorer代碼爲此..

看看這兩個鏈接..

  1. openintents

  2. Android-File-Explore

+0

另外這個... http://stackoverflow.com/questions/3592717/choose-file-dialog – user370305

+0

謝謝,我想通了,這是最好的解決方案! – Onheiron

1

大多數android發行版都沒有默認的文件瀏覽器,並且您注意到的行爲是默認的android行爲。如果安裝了任何優秀的第三方文件瀏覽器,它將自動顯示在該列表中。但不保證每個最終用戶都將安裝文件瀏覽器。可以爲此創建一個通用碎片小部件(可能與其他人共享)。

1
public void loaddile() 
    { 
    private static final int gallery=12; 
    String type="*/*"; 

Intent i=new Intent(Intent.ACTION_GET_CONTENT); 
    i.setType(type); 
startActivityForResult(Intent.createChooser(i,"select file") ,gallery); 
} 


@Override 
protected void onActivityResult(int requestCode, int resultCode, Intent data) { 
    super.onActivityResult(requestCode, resultCode, data); 
    if (requestCode == gallery && resultCode == RESULT_OK && data != null) { 
     Uri uploadfileuri = data.getData(); 
     File file = new File(uploadfileuri.getPath()); 


    } 
} 
+1

請給出一些Ans的描述。 –