1.进入文件管理器
private voID intofileManager() { Intent intent = new Intent(Intent.ACTION_GET_CONTENT); intent.setType("*/*");//无类型限制// 有类型限制是这样的:// intent.setType(“image/*”);//选择图片// intent.setType(“audio/*”); //选择音频// intent.setType(“vIDeo/*”); //选择视频 (mp4 3gp 是androID支持的视频格式)// intent.setType(“vIDeo/*;image/*”);//同时选择视频和图片 intent.addcategory(Intent.category_OPENABLE); startActivityForResult(intent, 1); }
2.返回结果
@OverrIDe protected voID onActivityResult(int requestCode, int resultCode, @Nullable Intent data) { super.onActivityResult(requestCode, resultCode, data); String path; if (resultCode == Activity.RESulT_OK) { Uri uri = data.getData(); if ("file".equalsIgnoreCase(uri.getScheme())){//使用第三方应用打开 path = uri.getPath(); mfilePathTv.setText(path); Log.d("queryfilepath", "返回结果1: " + path); return; } if (Build.VERSION.SDK_INT > Build.VERSION_CODES.KITKAT) {//4.4以后 path = fileChooseUtil.getPath(this, uri); mfilePathTv.setText(path); Log.d("queryfilepath", "返回结果2: " + path); } else {//4.4以下下系统调用方法 path = fileChooseUtil.getRealPathFromURI(uri); mfilePathTv.setText(path); Log.d("queryfilepath", "返回结果3: " + path); } } }
3.用到的自定义工具类
public class fileChooseUtil { private static 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; } public static 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") public static String getPath(final Context context, final Uri uri) { final boolean isKitKat = Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT; // documentProvIDer if (isKitKat && documentsContract.isdocumentUri(context, uri)) { // ExternalStorageProvIDer if (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]; } } // DownloadsProvIDer else 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); } // MediaProvIDer else 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); } // file else if ("file".equalsIgnoreCase(uri.getScheme())) { uri.getPath(); } return null; } private static 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 static 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 static 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 static boolean isMediadocument(Uri uri) { return "com.androID.provIDers.media.documents".equals(uri.getAuthority()); }}
记得动态申请 sd 卡 等读写权限.
文件转载:
总结以上是内存溢出为你收集整理的Android 打开系统文件管理器,并返回选中文件的路径全部内容,希望文章能够帮你解决Android 打开系统文件管理器,并返回选中文件的路径所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)