aFileChooser:兼容android2.1+的文件選擇器
來自: http://www.jcodecraeer.com//a/anzhuokaifa/androidkaifa/2014/0821/1661.html
aFileChooser是一個簡化文件瀏覽與選擇過程的android項目。他主要是面向開發者,讓任何人都能輕易實現自己的文件瀏覽器或者文件選擇對話框。
intents使得第三方app在選擇系統內容資源成為可能,但是如果你想要用戶能根據自己的想法選擇任意一個文件,你必須調用一個已經存在的文件瀏覽器,但是很多手機廠商并沒有文件瀏覽器可用,因此很多應用的在遇到這個問題的時候往往是提示用戶安裝一個文件瀏覽器。aFileChooser的出現解決了這個問題,讓我們可以構件自己的文件瀏覽器。
他有幾個優點:
支持按分類選擇;
兼容api 7以上;
界面美觀;
可定制行強;
功能完善;
選擇文件目錄中的文件:

使用方法:
將aFileChooser的庫文件導入項目。
將FileChooserActivity寫入到你的AndroidManifest.xml文件中:
<activity
android:name="com.ipaulpro.afilechooser.FileChooserActivity"
android:icon="@drawable/ic_chooser"
android:enabled="@bool/use_activity"
android:exported="true"
android:label="@string/choose_file" >
<intent-filter>
<action android:name="android.intent.action.GET_CONTENT" />
<category android:name="android.intent.category.DEFAULT" />
<category android:name="android.intent.category.OPENABLE" />
<data android:mimeType="*/*" />
</intent-filter>
</activity> 注意android:exported="true"是必須的。
下面演示如何調用這個activity并獲得文件選擇器的返回值:
private static final int REQUEST_CHOOSER = 1234;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Create the ACTION_GET_CONTENT Intent
Intent getContentIntent = FileUtils.createGetContentIntent();
Intent intent = Intent.createChooser(getContentIntent, "Select a file");
startActivityForResult(intent, REQUEST_CHOOSER);
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
switch (requestCode) {
case REQUEST_CHOOSER:
if (resultCode == RESULT_OK) {
final Uri uri = data.getData();
// Get the File path from the Uri
String path = FileUtils.getPath(this, uri);
// Alternatively, use FileUtils.getFile(Context, Uri)
if (path != null && FileUtils.isLocal(path)) {
File file = new File(path);
}
}
break;
}
} 項目地址:https://github.com/iPaulPro/aFileChooser
更正:github上給出的demo中是使用FileUtils.createGetContentIntent()來得到Intent,在4.4中會彈出一個功能比較復雜的activity,實際上這是由系統提供的一個叫DocumentsUI的activity 跟這個項目一點關系都沒有,只不過他的調用與返回值形式和FileChooserActivity的調用與返回值形式一致。如果要用在4.4中使用FileChooserActivity的界面,不要使用FileUtils.createGetContentIntent()提供的intent,而是直接
Intent intent = new Intent(FileChooserExampleActivity.this,FileChooserActivity.class);
同時在FileChooserExampleActivity的AndroidManifest中將 android:enabled屬性去掉(demo中加了這一屬性)
當然 在4.4以下的版本中會彈出如下的chooser:

然后選擇select a file 就會進入到FileChooserActivity中。