Chinaunix首页 | 论坛 | 博客
  • 博客访问: 226202
  • 博文数量: 80
  • 博客积分: 0
  • 博客等级: 民兵
  • 技术积分: 781
  • 用 户 组: 普通用户
  • 注册时间: 2014-11-08 10:41
个人简介

爱莉清

文章分类

全部博文(80)

文章存档

2018年(1)

2017年(18)

2016年(49)

2015年(7)

2014年(5)

我的朋友

分类: Android平台

2016-04-19 16:03:57

使用系统自带的文件管理器进行文件的浏览和选择具体实现如下:
1、先看效果图:

2、添加文件浏览功能的库文件jar




这个库文件在这里提供:
afilechooser.zip

3、主要代码如下:

点击(此处)折叠或打开

  1. package com.nguhyw.chooserfile;



  2. import com.ipaulpro.afilechooser.utils.FileUtils;

  3. import android.app.Activity;
  4. import android.content.ActivityNotFoundException;
  5. import android.content.Intent;
  6. import android.net.Uri;
  7. import android.os.Bundle;
  8. import android.util.Log;
  9. import android.view.View;
  10. import android.view.View.OnClickListener;
  11. import android.widget.Button;
  12. import android.widget.Toast;

  13. public class MainActivity extends Activity {
  14.     
  15.     private static final String TAG = "FileChooserExampleActivity";
  16.     private static final int REQUEST_CODE = 6384;
  17.     private Button btnSelect;
  18.     protected void onCreate(Bundle savedInstanceState) {
  19.         super.onCreate(savedInstanceState);
  20.         setContentView(R.layout.activity_main);
  21.         
  22.         btnSelect = (Button) findViewById(R.id.btnSelect);
  23.         btnSelect.setOnClickListener(new View.OnClickListener() {
  24.             
  25.             public void onClick(View arg0) {
  26.                 showChooser();
  27.             }
  28.         });
  29.                 
  30.                 
  31.     }
  32.      private void showChooser() {
  33.         // Use the GET_CONTENT intent from the utility class
  34.         Intent target = FileUtils.createGetContentIntent();
  35.         // Create the chooser Intent
  36.         Intent intent = Intent.createChooser(
  37.                 target, getString(R.string.chooser_title));
  38.         try {
  39.             startActivityForResult(intent, REQUEST_CODE);
  40.         } catch (ActivityNotFoundException e) {
  41.             // The reason for the existence of aFileChooser
  42.         }
  43.     }
  44.     protected void onActivityResult(int requestCode, int resultCode, Intent data) {
  45.         switch (requestCode) {
  46.             case REQUEST_CODE:
  47.                 // If the file selection was successful
  48.                 if (resultCode == RESULT_OK) {
  49.                     if (data != null) {
  50.                         // Get the URI of the selected file
  51.                         final Uri uri = data.getData();
  52.                         Log.i(TAG, "Uri = " + uri.toString());
  53.                         try {
  54.                             // Get the file path from the URI
  55.                             final String path = FileUtils.getPath(this, uri);
  56.                             Toast.makeText(MainActivity.this,
  57.                                     "File Selected: " + path, Toast.LENGTH_LONG).show();
  58.                         } catch (Exception e) {
  59.                             Log.e("FileSelectorTestActivity", "File select error", e);
  60.                         }
  61.                     }
  62.                 }
  63.                 break;
  64.         }
  65.         super.onActivityResult(requestCode, resultCode, data);
  66.     }

  67.     
  68. }
布局文件:

点击(此处)折叠或打开

  1. <LinearLayout xmlns:android=""
  2.     xmlns:tools=""
  3.     android:layout_width="match_parent"
  4.     android:layout_height="match_parent"
  5.     android:orientation="vertical"
  6.      >

  7.     <Button
  8.         android:id="@+id/btnSelect"
  9.         android:layout_width="match_parent"
  10.         android:layout_height="wrap_content"
  11.         android:layout_alignParentRight="true"
  12.         android:layout_alignParentTop="true"
  13.         android:layout_marginTop="172dp"
  14.         android:text="选择文件" />

  15. </LinearLayout>

最重要的一点Manifest.xml文件中一定要添加权限不然会读取不到或者读取不完整:
android:exported="true"绝对不能少。

点击(此处)折叠或打开

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <manifest xmlns:android=""
  3.     package="com.nguhyw.chooserfile"
  4.     android:versionCode="1"
  5.     android:versionName="1.0" >

  6.     <uses-sdk
  7.         android:minSdkVersion="8"
  8.         android:targetSdkVersion="19" />
  9.      <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
  10.     <application
  11.         android:allowBackup="true"
  12.         android:icon="@drawable/ic_launcher"
  13.         android:label="@string/app_name"
  14.         android:theme="@style/AppTheme" >
  15.         <activity
  16.             android:name="com.nguhyw.chooserfile.MainActivity"
  17.             android:label="@string/app_name"
  18.             android:exported="true" >
  19.             <intent-filter>
  20.                 <action android:name="android.intent.action.MAIN" />

  21.                 <category android:name="android.intent.category.LAUNCHER" />
  22.             </intent-filter>
  23.         </activity>
  24.     </application>

  25. </manifest>

3、工程文件:
文件选择器.zip


阅读(5975) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~