記一次文件上傳引發的血案。
解決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 context
* @param file
*
* @return
*/
public static Uri getUriFromFile(Context context, File file) {
if (context == null || file == null) {
throw new NullPointerException();
}
Uri uri;
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
uri = FileProvider.getUriForFile(context.getApplicationContext(), BuildConfig.APPLICATION_ID + ".fileprovider", file);
} else {
uri = Uri.fromFile(file);
}
return uri;
}
由于7.0的升級還需要在AndroidManifest.xml
中配置FileProvider
<provider
android:name="android.support.v4.content.FileProvider"
android:authorities="${applicationId}.fileprovider"
android:exported="false"
android:grantUriPermissions="true">
<meta-data
android:name="android.support.FILE_PROVIDER_PATHS"
android:resource="@xml/file_paths" />
</provider>
${applicationId}.fileprovider
這個配置要記牢,后期遇到大坑就靠這個值了。
xml/file_paths
文件如下:
<!--物理路徑相當于Context.getFilesDir() + /path/-->
<files-path name="name" path="path" />
<!--物理路徑相當于Context.getCacheDir() + /path/-->
<cache-path name="name" path="path" />
<!-- 物理路徑相當于Environment.getExternalStorageDirectory() + /path/-->
<external-path name="name" path="path" />
<!-- 物理路徑相當于Context.getExternalFilesDir(String) + /path/-->
<external-files-path name="name" path="path" />
<!-- 物理路徑相當于Context.getExternalCacheDir() + /path/-->
<external-cache-path name="name" path="path" />
將文件路徑轉(使用微信下載目錄做測試)為URI后,設置到Intent中。
/**
* 根據類型,加載文件選擇器
*/
private void chooseFileWithPath() {
Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
intent.addCategory(Intent.CATEGORY_OPENABLE);
//跳轉指定路徑,如果路徑不存在,切到sdcard
//需要讀權限
String path = getSDPath();
if (!TextUtils.isEmpty(path)) {
//使用微信下載目錄測試
path = path + File.separator + "tencent/MicroMsg/Download";
File file = new File(path);
if (file.exists()) {
//主要代碼
intent.setDataAndType(FileUtil.getUriFromFile(this, new File(path)), "*/*");
} else {
intent.setType("*/*");
}
} else {
intent.setType("*/*");
}
startActivityForResult(intent, REQUEST_CODE_FILE);
}
主要生效代碼:
Intent intent = new Intent(action,uri);
intent.setType("*/*");
startActivityForResult(intent, REQUEST_CODE_FILE);
或者
Intent intent = new Intent(action);
intent.setDataAndType(uri, "*/*");
startActivityForResult(intent, REQUEST_CODE_FILE);
特別注意:
指定目錄這種方式調起,在原生的管理器沒有其他注意的,但是如果用戶使用三方的管理器,例如QQ瀏覽器,那么在進入到指定目錄下,不可以執行返回操作。
例如:
如果路徑設置的是/sdcard/tencent/MicroMsg/Download
,在進入download目錄下,如果該目錄下沒有文件,那么想返回到其上層目錄MicroMsg,是不可以的。
4. 設置多種文件類型
通過intent.setType()
方式設置的文件類型,只能生效一次,所以如果想只選擇doc、excel、pdt、ppt
等辦公文檔,過濾掉其他文件,就不能使用intent .setType()
方式,而是使用Intent.EXTRA_MIME_TYPES
來傳值。
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";
/**
* 多種文件類型
*/
private void chooseMoreTypes() {
Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
intent.addCategory(Intent.CATEGORY_OPENABLE);
String[] mimeTypes = {DOC, DOCX, PDF, PPT, PPTX, XLS, XLS1, XLSX};
intent.setType("application/*");
intent.putExtra(Intent.EXTRA_MIME_TYPES, mimeTypes);
startActivityForResult(intent, REQUEST_CODE_FILE);
}
以上是準備工作,調起文件管理器進行選擇文件,對于一個Android開發者來說,以上步驟只是相當于一小步,不要忘記適配。
接下來才是長征路。
5. URI轉路徑
在這一步主要解決的是將返回的URI轉換為File,大坑也往往就在這一步出現。
從網上能找到File轉File的代碼,但是百度出來的內容,10篇有8篇是一樣的,剩下2篇不能看。
但是這8篇中幾乎都是相同的,沒有解決一個至關重要的問題QQ文件管理器。
也可能是不會用搜索引擎吧。
上代碼
/**
* 專為Android4.4設計的從Uri獲取文件絕對路徑,以前的方法已不好使
*/
@RequiresApi(api = Build.VERSION_CODES.KITKAT)
public static String getPath(final Context context, final Uri uri) {
final boolean isKitKat = Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT;
if (isKitKat && DocumentsContract.isDocumentUri(context, uri)) {
//一些三方的文件瀏覽器會進入到這個方法中,例如ES
//QQ文件管理器不在此列
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)) {
...
}
// MediaProvider
else if (isMediaDocument(uri)) {
...
}
} else if ("content".equalsIgnoreCase(uri.getScheme())) {// MediaStore (and general)
return getDataColumn(context, uri, null, null);
} else if ("file".equalsIgnoreCase(uri.getScheme())) {// File
...
}
return null;
}
發現部手機會有第三方的文件管理器,如ES,QQ等目前只接觸到這兩種,不排除其他APP,相信大部分都是好同志,但不限于鵝廠大佬。
通過QQ瀏覽器選擇到的文件,則會報出不存在_data字段這個錯誤。
WTF
懵逼.jpg
返回的URI中uri.getAuthority()
的值并不是自己在Manifest.xml中設置的${applicationId}.fileprovider而是com.tencent.mtt.fileprovider。
這時候,前面搜索到的文章,幾乎都沒有解決這個問題。
鑒于水平有限,被坑了半天后。
通過分析uri.getPath();
的值。
content://com.tencent.mtt.fileprovider/QQBrowser/demo.mp4
寫下來如下代碼,如各位大佬有更好的解決方案,望轉告。
//判斷QQ文件管理器
if (isQQMediaDocument(uri)) {
String path = uri.getPath();
File fileDir = Environment.getExternalStorageDirectory();
File file = new File(fileDir, path.substring("/QQBrowser".length(), path.length()));
return file.exists() ? file.toString() : null;
}
測試通過機型: 魅族15,三星9300,mi6,oppoR9, 華為mate9
缺失代碼自行補齊,
主要類代碼FileUtil
其他問題
1. 文件類型設置
產品需求:
╔══════════════════════════════
║
║ 上傳文件 上傳視頻
║
╚══════════════════════════════
要求: 1. 點擊視頻選擇mp4,2. 點擊文件選擇pdf、word等office文件。
在設置Intent的時候分為2種:
-
intent.setType("*/*")
這種情況下,本意是想選擇office文檔。通過Intent.EXTRA_MIME_TYPES
來限制文件類型,但是這種情況下,會出現第三方的文件管理器,而三方的一些情況下不會生效,所有文件都可以選擇。
我做的是,通過觀察MIME類型,我設置的是application/*
第三方的文件管理圖標隱藏掉了,只能通過系統的文件管理選擇文件。
intent.setType("video/mp4);
- 這種會顯示三方文件管理器,但是會過濾掉其他的文件,只有video類型的,如果有avi類型,那么還需要在
onActivityResult
中判斷文件后綴名。 - 系統的文件管理器會生效,只能選擇
Intent.EXTRA_MIME_TYPES
設置的類型。
2. 返回URI的問題
- 從文件管理器選擇文件,返回的URI是
content://com.android.externalstorage.documents/document/primary/update/A5679B1.mp4
- 從『視頻』選擇文件,返回的URI是
content://com.android.providers.media.documents/document/video:5188
遇到的問題:
判斷文件格式是否是我設置的類型,如果intent.setType("video/*");
,但是只想要"mp4"格式的文件,那么在onActivityResult
中通過返回的數據進行判斷,前期想的是通過uri.getLastPathsegment()
,判斷文件的后綴名,但是后來測試遇到了第二種情況,從『視頻』里選擇到文件,這時返回URI不符合規則了,所以偷懶是不行的,只能通過轉換,將源文件的名稱,判斷后綴名。
歡迎留言提問,共同進步