android 把file资源转file,Android文件管理器选择文件,获得文件路径URI转File

本文介绍了在Android中如何从文件管理器选择文件并转换URI为File,涉及MIME类型、文件路径转URI、指定浏览位置、设置多种文件类型以及处理QQ浏览器的特殊问题。详细讲解了处理过程中可能遇到的坑和解决方案。
摘要由CSDN通过智能技术生成

记一次文件上传引发的血案。

解决QQ浏览器com.tencent.mtt.fileprovider问题。

更新列表

日期

修改内容

2019年7月2日

更新遇到的问题

前情描述:

使用系统文件管理器,选择指定文件类型上传。

基础知识

MIME

调起文件管理器

指定浏览位置(路径转URI)

设置多种文件类型

URI转路径

踩坑

com.tencent.mtt.fileprovider 问题

1. MIME

MIME (Multipurpose Internet Mail Extensions) 是描述消息内容类型的因特网标准。

final String DOC = "application/msword";

final String XLS = "application/vnd.ms-excel";

final String PPT = "application/vnd.ms-powerpoint";

final String DOCX = "application/vnd.openxmlformats-officedocument.wordprocessingml.document";

final String XLSX = "application/x-excel";

final String XLSX = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet";

final String PPTX = "application/vnd.openxmlformats-officedocument.presentationml.presentation";

final String PDF = "application/pdf";

final String MP4 = "video/mp4";

final String M3U8 = "application/x-mpegURL";

更多文件类型,自行百度

2. 调起文件管理器

所有类型文件

Intent intent = new Intent(Intent.ACTION_GET_CONTENT);

//任意类型文件

intent.setType("*/*");

intent.addCategory(Intent.CATEGORY_OPENABLE);

startActivityForResult(intent,1);

//-------常用类型

//图片

//intent.setType(“image/*”);

//音频

//intent.setType(“audio/*”);

//视频

//intent.setType(“video/*”);

//intent.setType(“video/*;image/*”);

系统的相冊

Intent intent= new Intent(Intent.ACTION_PICK, null);

intent.setDataAndType(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, "image/*");

startActivityForResult(intent, REQUEST_CODE_FILE);

3. 指定浏览位置(路径转URI)

跳转到指定路径下,涉及到将路径转为URI,考虑Android版本区别

/**

* file --> uri

* @param c

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Android中,获取Uri的真实路径换成File对象可以使用以下代码: ```java public static File getFileFromUri(Context context, Uri uri) { String filePath = null; if (DocumentsContract.isDocumentUri(context, uri)) { // 如果是Document类型的Uri,则通过Document ID来进行解析 String documentId = DocumentsContract.getDocumentId(uri); if ("com.android.providers.media.documents".equals(uri.getAuthority())) { // MediaProvider的Uri String[] split = documentId.split(":"); 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; } String selection = MediaStore.Images.Media._ID + "=?"; String[] selectionArgs = new String[]{split[1]}; filePath = getDataColumn(context, contentUri, selection, selectionArgs); } else if ("com.android.providers.downloads.documents".equals(uri.getAuthority())) { // DownloadsProvider的Uri Uri contentUri = ContentUris.withAppendedId(Uri.parse("content://downloads/public_downloads"), Long.valueOf(documentId)); filePath = getDataColumn(context, contentUri, null, null); } else if ("com.android.externalstorage.documents".equals(uri.getAuthority())) { // ExternalStorageProvider的Uri String[] split = documentId.split(":"); if ("primary".equalsIgnoreCase(split[0])) { filePath = Environment.getExternalStorageDirectory() + "/" + split[1]; } } } else if ("content".equalsIgnoreCase(uri.getScheme())) { // 如果是普通Content类型的Uri,则直接查询该Uri对应的数据 filePath = getDataColumn(context, uri, null, null); } else if ("file".equalsIgnoreCase(uri.getScheme())) { // 如果是File类型的Uri,则直接获取File路径 filePath = uri.getPath(); } return new File(filePath); } private static String getDataColumn(Context context, Uri uri, String selection, String[] selectionArgs) { String[] projection = {MediaStore.Images.Media.DATA}; Cursor cursor = null; try { cursor = context.getContentResolver().query(uri, projection, selection, selectionArgs, null); if (cursor != null && cursor.moveToFirst()) { int columnIndex = cursor.getColumnIndexOrThrow(projection[0]); return cursor.getString(columnIndex); } } finally { if (cursor != null) { cursor.close(); } } return null; } ``` 其中,getDataColumn方法用于查询指定Uri对应的文件路径,getFileFromUri方法则将Uri换为File对象。需要注意的是,不同类型的Uri需要进行不同的解析方式,具体可以根据代码注释进行理解。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值