android使用外部文件管理获取文件路径

来源:互联网 发布:2016淘宝618大促报名 编辑:程序博客网 时间:2024/05/17 14:29

打开设备已经安装的文件管理器  FILE_SELECT_CODE自己定义的code值 int类型

private void importExcel() {Intent intent = new Intent(Intent.ACTION_GET_CONTENT);intent.setType("*/*");intent.addCategory(Intent.CATEGORY_OPENABLE);try {startActivityForResult(Intent.createChooser(intent, "请选择一个要上传的文件"), FILE_SELECT_CODE);} catch (ActivityNotFoundException ex) {Toast.makeText(this, R.string.s_pleaseInstallTheFileManager, Toast.LENGTH_SHORT).show();// 可以连接到下载文件管理器的连接让用户下载文件管理器}}


 

重写onActivityResult方法,在返回值中获取并解析路径

@Overrideprotected void onActivityResult(int requestCode, int resultCode, Intent data) {if (resultCode == Activity.RESULT_OK && requestCode == FILE_SELECT_CODE) {Uri uri = data.getData();String[] proj = { MediaStore.Images.Media.DATA };Cursor actualimagecursor = managedQuery(uri, proj, null, null, null);int actual_image_column_index = actualimagecursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);actualimagecursor.moveToFirst();String path = actualimagecursor.getString(actual_image_column_index);// 获取选择文件的路径HashMap<String, String> hashMap = Utils.getTestRoomsDataForExcel(path);}super.onActivityResult(requestCode, resultCode, data);}

 

上面代码path值 就是选择文件的路径  知道文件的路径之后就.......

 

 

这个方法有些文件管理不支持,如果在下面那句代码中报有异常(空指针)的话.应该是文件管理不支持

int actual_image_column_index = actualimagecursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);

 

 

 

原创粉丝点击