700字范文,内容丰富有趣,生活中的好帮手!
700字范文 > Android调用系统自带的文件管理器进行文件选择

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

时间:2024-04-07 07:59:44

相关推荐

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

这几天在做的项目网盘。上传时需要用到调用系统自带的文件管理器来选择文件,后来就在考虑怎么调用,网上也搜了很久,没有很好的解决方法,后来在一瞬间发现了一篇不错的文章,借鉴了一下代码。

[java]view plaincopy/**调用文件选择软件来选择文件**/ privatevoidshowFileChooser(){ intent=newIntent(Intent.ACTION_GET_CONTENT); intent.setType("*/*"); intent.addCategory(Intent.CATEGORY_OPENABLE); try{ startActivityForResult(Intent.createChooser(intent,"请选择一个要上传的文件"), FILE_SELECT_CODE); }catch(android.content.ActivityNotFoundExceptionex){ //PotentiallydirecttheusertotheMarketwithaDialog Toast.makeText(getActivity(),"请安装文件管理器",Toast.LENGTH_SHORT) .show(); } }

在catch,我们可以做更多的操作,比如会跳转到一个下载文件管理器的页面或者等等。

对于返回的数据怎么处理呢。我项目中的上传是如下接收

[java]view plaincopy/**根据返回选择的文件,来进行上传操作**/ @Override publicvoidonActivityResult(intrequestCode,intresultCode,Intentdata){ //TODOAuto-generatedmethodstub if(resultCode==Activity.RESULT_OK){ //GettheUrioftheselectedfile Uriuri=data.getData(); Stringurl; try{ url=FFileUtils.getPath(getActivity(),uri); Log.i("ht","url"+url); StringfileName=url.substring(url.lastIndexOf("/")+1); intent=newIntent(getActivity(),UploadServices.class); intent.putExtra("fileName",fileName); intent.putExtra("url",url); intent.putExtra("type",""); intent.putExtra("fuid",""); intent.putExtra("type",""); getActivity().startService(intent); }catch(URISyntaxExceptione){ //TODOAuto-generatedcatchblock e.printStackTrace(); } } super.onActivityResult(requestCode,resultCode,data); }

---------------------------------------------------------

[java]view plaincopyprivatestaticfinalintFILE_SELECT_CODE=0; privatestaticfinalStringTAG="VideoActivity"; privatevoidchooseFile(){ Intentintent=newIntent(Intent.ACTION_GET_CONTENT); intent.setType("*/*"); intent.addCategory(Intent.CATEGORY_OPENABLE); try{ startActivityForResult(Intent.createChooser(intent,"选择文件"),FILE_SELECT_CODE); }catch(android.content.ActivityNotFoundExceptionex){ Toast.makeText(this,"亲,木有文件管理器啊-_-!!",Toast.LENGTH_SHORT).show(); } } @Override publicvoidonActivityResult(intrequestCode,intresultCode,Intentdata){ //TODOAuto-generatedmethodstub if(resultCode!=Activity.RESULT_OK){ Log.e(TAG,"onActivityResult()error,resultCode:"+resultCode); super.onActivityResult(requestCode,resultCode,data); return; } if(requestCode==FILE_SELECT_CODE){ Uriuri=data.getData(); Log.i(TAG,"------->"+uri.getPath()); } super.onActivityResult(requestCode,resultCode,data); }

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。