Android实现微信和QQ“在其他应用打开”列表中添加自己的应用,并获取文件路径。
实现例如在微信QQ通过选中某个文件,选择其他应用的打开方式。效果如下:
1.在AndroidManifests中添加:
其中minType代表要打开的文件类型。
常用的如:
{".3gp", "video/3gpp"},
{".apk", "application/vnd.android.package-archive"},
{".asf", "video/x-ms-asf"},
{".avi", "video/x-msvideo"},
{".bin", "application/octet-stream"},
{".bmp", "image/bmp"},
{".c", "text/plain"},
{".class", "application/octet-stream"},
{".conf", "text/plain"},
{".cpp", "text/plain"},
{".doc", "application/msword"},
{".docx", "application/vnd.openxmlformats-officedocument.wordprocessingml.document"},
{".xls", "application/vnd.ms-excel"},
{".xlsx", "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"},
{".exe", "application/octet-stream"},
{".gif", "image/gif"},
{".gtar", "application/x-gtar"},
{".gz", "application/x-gzip"},
{".h", "text/plain"},
{".htm", "text/html"},
{".html", "text/html"},
{".jar", "application/java-archive"},
{".java", "text/plain"},
{".jpeg", "image/jpeg"},
{".jpg", "image/jpeg"},
{".js", "application/x-javascript"},
{".log", "text/plain"},
{".m3u", "audio/x-mpegurl"},
{".m4a", "audio/mp4a-latm"},
{".m4b", "audio/mp4a-latm"},
{".m4p", "audio/mp4a-latm"},
{".m4u", "video/vnd.mpegurl"},
{".m4v", "video/x-m4v"},
{".mov", "video/quicktime"},
{".mp2", "audio/x-mpeg"},
{".mp3", "audio/x-mpeg"},
{".mp4", "video/mp4"},
{".mpc", "application/vnd.mpohun.certificate"},
{".mpe", "video/mpeg"},
{".mpeg", "video/mpeg"},
{".mpg", "video/mpeg"},
{".mpg4", "video/mp4"},
{".mpga", "audio/mpeg"},
{".msg", "application/vnd.ms-outlook"},
{".ogg", "audio/ogg"},
{".pdf", "application/pdf"},
{".png", "image/png"},
{".pps", "application/vnd.ms-powerpoint"},
{".ppt", "application/vnd.ms-powerpoint"},
{".pptx", "application/vnd.openxmlformats-officedocument.presentationml.presentation"},
{".prop", "text/plain"},
{".rc", "text/plain"},
{".rmvb", "audio/x-pn-realaudio"},
{".rtf", "application/rtf"},
{".sh", "text/plain"},
{".tar", "application/x-tar"},
{".tgz", "application/x-compressed"},
{".txt", "text/plain"},
{".wav", "audio/x-wav"},
{".wma", "audio/x-ms-wma"},
{".wmv", "audio/x-ms-wmv"},
{".wps", "application/vnd.ms-works"},
{".xml", "text/plain"},
{".z", "application/x-compress"},
{".zip", "application/x-zip-compressed"},
{"", "*/*"}
2.获取路径
Intent intent = getIntent(); String action = intent.getAction();if(intent.ACTION_VIEW.equals(action)){Log.v(intent.getDataString());}
"intent.getDataString()"返回的就是文件路径,但是会有编码的问题,需要用decode处理一下
Intent intent = getIntent();String action = intent.getAction();if (intent1.ACTION_VIEW.equals(action)) { Uri uri = intent.getData(); String str = Uri.decode(uri.getEncodedPath());}
在自己文件管理选择本应用打开时,url的值为content://media/external/file/85139
在微信中选择本应用打开时,url的值为
content://com.tencent.mm.external.fileprovider/external/tencent/MicroMsg/Download/111.doc
在QQ中选择本应用打开时,url的值为
file:///storage/emulated/0/tencent/MicroMsg/Download/111.doc
获取本地文件file格式路径可以直接使用,uri需要转成file格式,这里分两种情况:
如果是媒体库查询的uri,如content://media/external/file/85139 格式的转file方法:
public static String getFilePathFromContentUri(Uri selectedVideoUri, Activity context) { String filePath = ""; String[] filePathColumn = {MediaStore.MediaColumns.DATA};// Cursor cursor = contentResolver.query(selectedVideoUri, filePathColumn, null, null, null);// 也可用下面的方法拿到cursor Cursor cursor = context.managedQuery(selectedVideoUri, filePathColumn, null, null, null); if (cursor != null) { cursor.moveToFirst(); int columnIndex = cursor.getColumnIndex(filePathColumn[0]); filePath = cursor.getString(columnIndex); try { //4.0以上的版本会自动关闭 (4.0--14;; 4.0.3--15) if (Integer.parseInt(Build.VERSION.SDK) < 14) { cursor.close(); } } catch (Exception e) { Log.e("转换地址", "error:" + e); } } return filePath; }
如果是fileprovider提供的contenturi,如content://com.tencent.mm.external.fileprovider/external/tencent/MicroMsg/Download/111.doc 要转换成file格式方法(这里参照并借鉴 https://www.jianshu.com/p/0ca6989f2bc2):
public static String getFPUriToPath(Context context, Uri uri) { try { List packs = context.getPackageManager().getInstalledPackages(PackageManager.GET_PROVIDERS); if (packs != null) { String fileProviderClassName = FileProvider.class.getName(); for (PackageInfo pack : packs) { ProviderInfo[] providers = pack.providers; if (providers != null) { for (ProviderInfo provider : providers) { if (uri.getAuthority().equals(provider.authority)) { if (provider.name.equalsIgnoreCase(fileProviderClassName)) { Class fileProviderClass = FileProvider.class; try { Method getPathStrategy = fileProviderClass.getDeclaredMethod("getPathStrategy", Context.class, String.class); getPathStrategy.setAccessible(true); Object invoke = getPathStrategy.invoke(null, context, uri.getAuthority()); if (invoke != null) { String PathStrategyStringClass = FileProvider.class.getName() + "$PathStrategy"; Class<?> PathStrategy = Class.forName(PathStrategyStringClass); Method getFileForUri = PathStrategy.getDeclaredMethod("getFileForUri", Uri.class); getFileForUri.setAccessible(true); Object invoke1 = getFileForUri.invoke(invoke, uri); if (invoke1 instanceof File) { String filePath = ((File) invoke1).getAbsolutePath(); return filePath; } } } catch (NoSuchMethodException e) { e.printStackTrace(); } catch (InvocationTargetException e) { e.printStackTrace(); } catch (IllegalAccessException e) { e.printStackTrace(); } catch (ClassNotFoundException e) { e.printStackTrace(); } break; } break; } } } } } } catch (Exception e) { e.printStackTrace(); } return null; }
更多相关文章
- Android原生(Native)C开发之六:libpng移植笔记
- 打开Android(安卓)PVLogger的方法
- 解决打开Android(安卓)SDK Manager时出现“.....\sdk\tools\l
- Android数据格式之解析Json
- Google Map Android(安卓)v2开发: 安装运行Google Map Android(
- Android(安卓)UI设计——Spinner控件
- Android工程import进workspace常见问题
- android学习日记之intent与IPC
- android eclipse环境搭建