使用系统自带的文件管理器进行文件的浏览和选择具体实现如下:
1、先看效果图:
2、添加文件浏览功能的库文件jar
这个库文件在这里提供:
afilechooser.zip
3、主要代码如下:
-
package com.nguhyw.chooserfile;
-
-
-
-
import com.ipaulpro.afilechooser.utils.FileUtils;
-
-
import android.app.Activity;
-
import android.content.ActivityNotFoundException;
-
import android.content.Intent;
-
import android.net.Uri;
-
import android.os.Bundle;
-
import android.util.Log;
-
import android.view.View;
-
import android.view.View.OnClickListener;
-
import android.widget.Button;
-
import android.widget.Toast;
-
-
public class MainActivity extends Activity {
-
-
private static final String TAG = "FileChooserExampleActivity";
-
private static final int REQUEST_CODE = 6384;
-
private Button btnSelect;
-
protected void onCreate(Bundle savedInstanceState) {
-
super.onCreate(savedInstanceState);
-
setContentView(R.layout.activity_main);
-
-
btnSelect = (Button) findViewById(R.id.btnSelect);
-
btnSelect.setOnClickListener(new View.OnClickListener() {
-
-
public void onClick(View arg0) {
-
showChooser();
-
}
-
});
-
-
-
}
-
private void showChooser() {
-
// Use the GET_CONTENT intent from the utility class
-
Intent target = FileUtils.createGetContentIntent();
-
// Create the chooser Intent
-
Intent intent = Intent.createChooser(
-
target, getString(R.string.chooser_title));
-
try {
-
startActivityForResult(intent, REQUEST_CODE);
-
} catch (ActivityNotFoundException e) {
-
// The reason for the existence of aFileChooser
-
}
-
}
-
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
-
switch (requestCode) {
-
case REQUEST_CODE:
-
// If the file selection was successful
-
if (resultCode == RESULT_OK) {
-
if (data != null) {
-
// Get the URI of the selected file
-
final Uri uri = data.getData();
-
Log.i(TAG, "Uri = " + uri.toString());
-
try {
-
// Get the file path from the URI
-
final String path = FileUtils.getPath(this, uri);
-
Toast.makeText(MainActivity.this,
-
"File Selected: " + path, Toast.LENGTH_LONG).show();
-
} catch (Exception e) {
-
Log.e("FileSelectorTestActivity", "File select error", e);
-
}
-
}
-
}
-
break;
-
}
-
super.onActivityResult(requestCode, resultCode, data);
-
}
-
-
-
}
布局文件:
-
<LinearLayout xmlns:android=""
-
xmlns:tools=""
-
android:layout_width="match_parent"
-
android:layout_height="match_parent"
-
android:orientation="vertical"
-
>
-
-
<Button
-
android:id="@+id/btnSelect"
-
android:layout_width="match_parent"
-
android:layout_height="wrap_content"
-
android:layout_alignParentRight="true"
-
android:layout_alignParentTop="true"
-
android:layout_marginTop="172dp"
-
android:text="选择文件" />
-
-
</LinearLayout>
最重要的一点Manifest.xml文件中一定要添加权限不然会读取不到或者读取不完整:
android:exported="true"绝对不能少。
-
<?xml version="1.0" encoding="utf-8"?>
-
<manifest xmlns:android=""
-
package="com.nguhyw.chooserfile"
-
android:versionCode="1"
-
android:versionName="1.0" >
-
-
<uses-sdk
-
android:minSdkVersion="8"
-
android:targetSdkVersion="19" />
-
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
-
<application
-
android:allowBackup="true"
-
android:icon="@drawable/ic_launcher"
-
android:label="@string/app_name"
-
android:theme="@style/AppTheme" >
-
<activity
-
android:name="com.nguhyw.chooserfile.MainActivity"
-
android:label="@string/app_name"
-
android:exported="true" >
-
<intent-filter>
-
<action android:name="android.intent.action.MAIN" />
-
-
<category android:name="android.intent.category.LAUNCHER" />
-
</intent-filter>
-
</activity>
-
</application>
-
-
</manifest>
3、工程文件:
文件选择器.zip
阅读(6079) | 评论(0) | 转发(0) |