Android调用系统自带的文件管理器进行文件选择

来源:互联网 发布:linux查看存储 编辑:程序博客网 时间:2024/05/21 08:49

思路和代码学习并转自:http://blog.csdn.net/zqchn

 

 

/** 调用文件选择软件来选择文件 **/private void showFileChooser() {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 DialogToast.makeText(getActivity(), "请安装文件管理器", Toast.LENGTH_SHORT).show();}}


 

 

返回的数据处理示例:

 

/** 根据返回选择的文件,来进行上传操作 **/@Overridepublic void onActivityResult(int requestCode, int resultCode, Intent data) {// TODO Auto-generated method stubif (resultCode == Activity.RESULT_OK) {// Get the Uri of the selected fileUri uri = data.getData();String url;try {url = FFileUtils.getPath(getActivity(), uri);Log.i("ht", "url" + url);String fileName = url.substring(url.lastIndexOf("/") + 1);intent = new Intent(getActivity(), UploadServices.class);intent.putExtra("fileName", fileName);intent.putExtra("url", url);intent.putExtra("type ", "");intent.putExtra("fuid", "");intent.putExtra("type", "");getActivity().startService(intent);} catch (URISyntaxException e) {// TODO Auto-generated catch blocke.printStackTrace();}}super.onActivityResult(requestCode, resultCode, data);}


 

 

 

 

原创粉丝点击