一、打开系统自带的文件管理器

       if (!Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) {showToast(R.string.msg_storage_nosdcard);return;}//获取文件下载路径String compName = AppString.getCompanyName();String path = Environment.getExternalStorageDirectory().getAbsolutePath() + "/" + compName + "/OSC_DATA/";File dir = new File(path);if (!dir.exists()) {dir.mkdirs();}//调用系统文件管理器打开指定路径目录Intent intent = new Intent(Intent.ACTION_GET_CONTENT);intent.setDataAndType(Uri.fromFile(dir.getParentFile()), "file/*.txt");intent.addCategory(Intent.CATEGORY_OPENABLE);startActivityForResult(intent, REQUEST_CHOOSEFILE);
@Override
protected void onActivityResult(int requestCode,int resultCode,Intent data){//选择文件返回super.onActivityResult(requestCode,resultCode,data);if(resultCode==RESULT_OK){switch(requestCode){case REQUEST_CHOOSEFILE:Uri uri=data.getData();chooseFilePath=FileChooseUtil.getInstance(this).getChooseFileResultPath(uri);Log.d(TAG,"选择文件返回:"+chooseFilePath);sendFileMessage(chooseFilePath);break;}}
}

二、工具类FileChooseUtil

public class FileChooseUtil {private Context context;private static FileChooseUtil util = null;private FileChooseUtil(Context context) {this.context = context;}public static FileChooseUtil getInstance(Context context) {if (util == null) {util = new FileChooseUtil(context);}return util;}/*** 对外接口  获取uri对应的路径** @param uri* @return*/public String getChooseFileResultPath(Uri uri) {String chooseFilePath = null;if ("file".equalsIgnoreCase(uri.getScheme())) {//使用第三方应用打开chooseFilePath = uri.getPath();Toast.makeText(context, chooseFilePath, Toast.LENGTH_SHORT).show();return chooseFilePath;}if (Build.VERSION.SDK_INT > Build.VERSION_CODES.KITKAT) {//4.4以后chooseFilePath = getPath(context, uri);} else {//4.4以下下系统调用方法chooseFilePath = getRealPathFromURI(uri);}return chooseFilePath;}private String getRealPathFromURI(Uri contentUri) {String res = null;String[] proj = {MediaStore.Images.Media.DATA};Cursor cursor = context.getContentResolver().query(contentUri, proj, null, null, null);if (null != cursor && cursor.moveToFirst()) {int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);res = cursor.getString(column_index);cursor.close();}return res;}/*** 专为Android4.4设计的从Uri获取文件绝对路径,以前的方法已不好使*/@SuppressLint("NewApi")private String getPath(final Context context, final Uri uri) {final boolean isKitKat = Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT;// DocumentProviderif (isKitKat && DocumentsContract.isDocumentUri(context, uri)) {// ExternalStorageProviderif (isExternalStorageDocument(uri)) {final String docId = DocumentsContract.getDocumentId(uri);final String[] split = docId.split(":");final String type = split[0];if ("primary".equalsIgnoreCase(type)) {return Environment.getExternalStorageDirectory() + "/" + split[1];}}// DownloadsProviderelse if (isDownloadsDocument(uri)) {final String id = DocumentsContract.getDocumentId(uri);final Uri contentUri = ContentUris.withAppendedId(Uri.parse("content://downloads/public_downloads"), Long.valueOf(id));return getDataColumn(context, contentUri, null, null);}// MediaProviderelse if (isMediaDocument(uri)) {final String docId = DocumentsContract.getDocumentId(uri);final String[] split = docId.split(":");final String type = split[0];Uri contentUri = null;if ("image".equals(type)) {contentUri = MediaStore.Images.Media.EXTERNAL_CONTENT_URI;} else if ("video".equals(type)) {contentUri = MediaStore.Video.Media.EXTERNAL_CONTENT_URI;} else if ("audio".equals(type)) {contentUri = MediaStore.Audio.Media.EXTERNAL_CONTENT_URI;}final String selection = "_id=?";final String[] selectionArgs = new String[]{split[1]};return getDataColumn(context, contentUri, selection, selectionArgs);}}// MediaStore (and general)else if ("content".equalsIgnoreCase(uri.getScheme())) {return getDataColumn(context, uri, null, null);}// Fileelse if ("file".equalsIgnoreCase(uri.getScheme())) {uri.getPath();}return null;}private String getDataColumn(Context context, Uri uri, String selection, String[] selectionArgs) {Cursor cursor = null;final String column = "_data";final String[] projection = {column};try {cursor = context.getContentResolver().query(uri, projection, selection, selectionArgs,null);if (cursor != null && cursor.moveToFirst()) {final int column_index = cursor.getColumnIndexOrThrow(column);return cursor.getString(column_index);}} finally {if (cursor != null)cursor.close();}return null;}/*** @param uri The Uri to check.* @return Whether the Uri authority is ExternalStorageProvider.*/private boolean isExternalStorageDocument(Uri uri) {return "com.android.externalstorage.documents".equals(uri.getAuthority());}/*** @param uri The Uri to check.* @return Whether the Uri authority is DownloadsProvider.*/private boolean isDownloadsDocument(Uri uri) {return "com.android.providers.downloads.documents".equals(uri.getAuthority());}/*** @param uri The Uri to check.* @return Whether the Uri authority is MediaProvider.*/private boolean isMediaDocument(Uri uri) {return "com.android.providers.media.documents".equals(uri.getAuthority());}
}

完!!!

Android调用系统自带的文件管理器,打开指定路径相关推荐

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

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

  2. Android调用系统自带的文件管理器获取图片绝对路径

    1.跳转到文件选择器进行文件选择 Intent intent = new Intent(Intent.ACTION_GET_CONTENT); intent.addCategory(Intent.CA ...

  3. 安卓 通过intent调用系统文件管理器打开指定路径目录

    安卓 通过intent调用系统文件管理器打开指定路径目录   转  https://blog.csdn.net/qq_34161388/article/details/78586247 当我们知道一个 ...

  4. UOS使用命令调用文件管理器打开指定路径的方法

    语法如下: dde-file-manager <路径> 例如: dde-file-manager /home/liumou/ 麒麟的方法如下: 麒麟kylin使用命令调用文件管理器打开指定 ...

  5. android调用文件管理打开某个路径,安卓 通过intent调用系统文件管理器打开指定路径目录...

    当我们知道一个文件的路径,如何调用系统文件管理器显示它的位置呢. 代码: private void openAssignFolder(String path){ File file = new Fil ...

  6. android安装自动打开网页,Android调用系统自带浏览器打开网页的实现方法

    Android调用系统自带浏览器打开网页的实现方法 在Android中可以调用自带的浏览器,或者指定一个浏览器来打开一个链接.只需要传入一个uri,可以是链接地址. 启动android默认浏览器 在A ...

  7. Android使用文件管理器打开指定文件夹,浏览里面的内容

    Android下可以打开一些文件,带有.doc 等后缀的文件网上一般都有解释,这个写一个使用文件管理器打开指定文件夹的 private void openAssignFolder(String pat ...

  8. Android 调用系统发短信界面,给指定号码发短信,并带短信内容

    工具类如下ContentUtil.java: package com.zhoucj.messagedemo.util; import android.content.Context; import a ...

  9. android调用系统自带的的浏览器搜索关键字

    //调用系统的浏览器搜索详情public void jumpBrowser(String value){/* 取得网页搜寻的intent */Intent search = new Intent(In ...

最新文章

  1. 77底盒和86底盒的区别_86型开关底盒的具体参数
  2. asp.net中GridView传多个值到其它页面的方法
  3. 神经网络参数量的计算:以UNet为例
  4. java中的slave_java – Jenkins slave在构建过程中脱机
  5. 天天象棋 残局闯关 第19关
  6. random_state的值如何选_同样是防晒衣,单价几十和几百块有什么不同?选防晒衣认准这4点...
  7. 跑三小时的monkey测试该怎么算_浅谈App测试(下)~带音频
  8. mysql 表锁的概念_MySQL 锁的一些简单概念
  9. react 中组件隐藏显示_React组件开发中常见的陷阱及解决
  10. delay 芯片时序output_set_input_delay/set_output_delay
  11. 学计算机应用技术应具备什么素养,2018年云南经济管理学院单招计算机应用技术职业适应性测试大纲...
  12. 最强AlphaGo怎样炼成?刚刚,DeepMind团队进行了全面解读
  13. 男单巅峰战林丹力克索尼 再度封王成功卫冕
  14. 英语数字的 android,英语数字听力手机版
  15. win7服务器设置远程连接数据库,高手亲自帮您windows7系统中SQL Server2008设置开启远程连接的操作图文教程...
  16. 自己收集的全国行政区划,具体到县级,不包括过直辖市和特别行政区
  17. mysql数据库对象是什么意思_数据库对象什么意思
  18. 美团外卖前端可视化界面组装平台 —— 乐高
  19. html手机支付案例,实例讲述Ecshop实现的支付宝手机网页支付功能
  20. 网站优化 SEO概念

热门文章

  1. Win10将某个软件/文件添加到开始屏幕(磁贴)
  2. 万字长文带你解读Linux
  3. CorelDRAW VBA - 段落文本内容的导出 ExportToFile 方法
  4. latex中文编号是问号的问题
  5. android温湿度传感节点指令源代码,Arduino+DHT11+OLED显示温湿度信息(附详细文档+源码)...
  6. Mac如何做才能彻底清理垃圾
  7. word 对齐方式 左对齐,居中,两端对齐 的区别 (图示)
  8. Ubuntu下图片转pdf和pdf合并
  9. 计算机组成原理 累加器实验
  10. 蘑菇街按关键字搜索mogujie商品 API 返回值说明