Android系统中一般自带文件浏览器,并开放给大家调用,现在简单实现该功能,后续文章将做详细应用。
1、布局文件
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity" >
<Button
android:id="@+id/bt1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="打开"/>"
</LinearLayout>
2、Java代码
package com.bjx.filetestexplorer;
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.Menu;
import android.view.View;
import android.widget.Toast;
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
findViewById(R.id.bt1).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
openSystemFile();
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
public void openSystemFile(){
Intent intent=new Intent(Intent.ACTION_GET_CONTENT);
//系统调用Action属性
intent.setType("*/*");
//设置文件类型
intent.addCategory(Intent.CATEGORY_OPENABLE);
// 添加Category属性
try{
startActivity(intent);
}catch(Exception e){
Toast.makeText(this, "没有正确打开文件管理器", 1).show();
}
}
}
3、注意
由于这里只是调用系统文件浏览器,没有文件读写等功能,无需添加读写权限。
Demo下载:
http://download.youkuaiyun.com/detail/renwudao24/8452857