唉,尝试了好半天了,才弄懂,
1、首先权限的问题
我们需要在根文件中添加权限:<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"></uses-permission>
2、上面两篇博客可以参考,一个告诉你如何查看文件的绝对路径,一个说明如何借助文件管理器选中文件
3、这里我的绝对路径是
final String FILE_NAME = "/storage/emulated/0/updatebin/update.txt"; 我把文件在手机里呢
4、点击按钮读文件内容,下面两个函数都行
btn = (Button) findViewById(R.id.button1);
btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
try {
dis.setText(readFileSdcardFile(FILE_NAME));
dis.setText(readSDFile(FILE_NAME));
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
});
}
public String readSDFile(String fileName) throws IOException {
File file = new File(fileName);
FileInputStream fis = new FileInputStream(file);
int length = fis.available();
byte [] buffer = new byte[length];
fis.read(buffer);
String res = EncodingUtils.getString(buffer, "UTF-8");
fis.close();
return res;
}
public String readFileSdcardFile(String fileName) throws IOException{
String res="";
try{
FileInputStream fin = new FileInputStream(fileName);
int length = fin.available();
byte [] buffer = new byte[length];
fin.read(buffer);
res = EncodingUtils.getString(buffer, "UTF-8");
fin.close();
}
catch(Exception e){
e.printStackTrace();
}
return res;
}
5、之前的博客里面说的函数也贴过来吧
在这个目录下 /storage/emulated/0/updatebin 搜索所有带keyword关键词的文件和文件夹 可以用来查看文件的绝对路径
权限:
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"></uses-permission>
private String searchFile(String keyword) {
String result = "";
File[] files = new File("/storage/emulated/0/updatebin").listFiles();
for (File file : files) {
if (file.getName().indexOf(keyword) >= 0) {
result += file.getPath() + "\n";
}
}
if (result.equals("")){
result = "找不到文件!!";
}
return result;
}
6、按钮下 可以调用管理器选择文件 并输出文件信息
打开文件管理器选择文件
private void showFileChooser() {
Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
intent.setType("*/*");
intent.addCategory(Intent.CATEGORY_OPENABLE);
try {
startActivityForResult(Intent.createChooser(intent, "请选择一个要上传的文件"),
FILE_SELECT_CODE);
} catch (android.content.ActivityNotFoundException ex) {
// Potentially direct the user to the Market with a Dialog
Toast.makeText(MainActivity, "请安装文件管理器", Toast.LENGTH_SHORT)
.show();
}
}
private void chooseFile() throws IOException {
Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
intent.setType("*/*");
intent.addCategory(Intent.CATEGORY_OPENABLE);
try {
startActivityForResult(Intent.createChooser(intent, "选择文件"), FILE_SELECT_CODE);
} catch (android.content.ActivityNotFoundException ex) {
Toast.makeText(this, "亲,木有文件管理器啊-_-!!", Toast.LENGTH_SHORT).show();
}
}
选择的文件的结果 这里输出文件绝对路径信息
Uri uri = data.getData(); Log.i(TAG, "------->" + uri.getPath());
@Override public void onActivityResult(int requestCode, int resultCode, Intent data) { // TODO Auto-generated method stub if (resultCode != Activity.RESULT_OK) { super.onActivityResult(requestCode, resultCode, data); return; } if (requestCode == FILE_SELECT_CODE) { Uri uri = data.getData(); Log.i(TAG, "------->" + uri.getPath()); // dis.setText(read()); //readFileOnLine(FILE_NAME); } super.onActivityResult(requestCode, resultCode, data); }
7、最后贴上自己完整的代码按钮点击 打开文件管理器 选择某个文件 并输出文件里面的内容
final String FILE_NAME = "/storage/emulated/0/updatebin/update.txt";
protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); dis=(TextView) findViewById(R.id.textView1); btn = (Button) findViewById(R.id.button1); btn.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { try { chooseFile();//按键点击打开文件管理器 } catch (IOException e) { e.printStackTrace(); } } }); } private void chooseFile() throws IOException { Intent intent = new Intent(Intent.ACTION_GET_CONTENT); intent.setType("*/*"); intent.addCategory(Intent.CATEGORY_OPENABLE); try { startActivityForResult(Intent.createChooser(intent, "选择文件"), FILE_SELECT_CODE); } catch (android.content.ActivityNotFoundException ex) { Toast.makeText(this, "亲,木有文件管理器啊-_-!!", Toast.LENGTH_SHORT).show(); } } @Override public void onActivityResult(int requestCode, int resultCode, Intent data) { // TODO Auto-generated method stub if (resultCode != Activity.RESULT_OK) { super.onActivityResult(requestCode, resultCode, data); return; } if (requestCode == FILE_SELECT_CODE) { Uri uri = data.getData(); Log.i(TAG, "------->" + uri.getPath()); try { //dis.setText(readFileSdcardFile(FILE_NAME)); dis.setText(readFileSdcardFile(uri.getPath())); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } super.onActivityResult(requestCode, resultCode, data); } public String readFileSdcardFile(String fileName) throws IOException{ String res=""; try{ FileInputStream fin = new FileInputStream(fileName); int length = fin.available(); byte [] buffer = new byte[length]; fin.read(buffer); res = EncodingUtils.getString(buffer, "UTF-8"); fin.close(); } catch(Exception e){ e.printStackTrace(); } return res; }