实现例如在微信QQ通过选中某个文件,选择其他应用的打开方式。效果如下:

1.在AndroidManifests中添加:

        <!--添加文件打开方式,可在第三方应用中将你的app加入打开方式列表--><activityandroid:name=".activity.PdfPreviewActivity" android:launchMode="singleTask"><intent-filter tools:ignore="AppLinkUrlError"><action android:name="android.intent.action.VIEW" /><category android:name="android.intent.category.DEFAULT" /><data android:mimeType="application/msword" /></intent-filter><intent-filter tools:ignore="AppLinkUrlError"><action android:name="android.intent.action.VIEW" /><category android:name="android.intent.category.DEFAULT" /><data android:mimeType="application/vnd.openxmlformats-officedocument.wordprocessingml.document" /></intent-filter><intent-filter tools:ignore="AppLinkUrlError"><action android:name="android.intent.action.VIEW" /><category android:name="android.intent.category.DEFAULT" /><data android:mimeType="application/vnd.ms-excel" /></intent-filter><intent-filter tools:ignore="AppLinkUrlError"><action android:name="android.intent.action.VIEW" /><category android:name="android.intent.category.DEFAULT" /><data android:mimeType="application/vnd.openxmlformats-officedocument.spreadsheetml.sheet" /></intent-filter><intent-filter tools:ignore="AppLinkUrlError"><action android:name="android.intent.action.VIEW" /><category android:name="android.intent.category.DEFAULT" /><data android:mimeType="application/vnd.ms-powerpoint" /></intent-filter><intent-filter tools:ignore="AppLinkUrlError"><action android:name="android.intent.action.VIEW" /><category android:name="android.intent.category.DEFAULT" /><data android:mimeType="application/vnd.openxmlformats-officedocument.presentationml.presentation" /></intent-filter><intent-filter tools:ignore="AppLinkUrlError"><action android:name="android.intent.action.VIEW" /><category android:name="android.intent.category.DEFAULT" /><data android:mimeType="application/pdf" /></intent-filter></activity>

其中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);
//      也可用下面的方法拿到cursorCursor 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<PackageInfo> 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<FileProvider> 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实现微信和QQ“在其他应用打开”列表中添加自己的应用,并获取文件路径。相关推荐

  1. Android 11 微信,QQ ,微博 分享适配

    Android 11 微信,QQ ,微博 分享适配 前言 微信篇 QQ篇 微博篇 前言 最近收到客服反馈 有用户反馈微信分享不了,具体询问一番发现是Android 11的小米机器,然后依次试了 QQ ...

  2. Android 接收微信、QQ其他应用打开,第三方分享

    这里给大家分享我在网上总结出来的一些知识,希望对大家有所帮助 在AndroidManifest.xml注册ACTION事件 <activityandroid:name="com.tes ...

  3. android 加号弹出菜单,Android仿微信、qq点击右上角加号弹出操作框

    Android仿微信.qq点击右上角加号弹出操作框,先上图,类似于下图这种,点击加号,会弹出一个对话框,如下图: 微信: 自己实现: 接下来,我们来实现此功能: 其实,实现原理就是,点击"+ ...

  4. Android自定义弹窗模仿微信,Android仿微信、qq点击右上角加号弹出操作框

    Android仿微信.qq点击右上角加号弹出操作框,先上图,类似于下图这种,点击加号,会弹出一个对话框,如下图: 微信: 自己实现: 接下来,我们来实现此功能: 其实,实现原理就是,点击"+ ...

  5. android 仿qq群头像,Android仿微信和QQ多图合并框架(类似群头像)的实现方法

    Android仿微信和QQ多图合并框架(类似群头像)的实现方法 发布时间:2020-10-21 10:33:03 来源:脚本之家 阅读:97 作者:jyb_96 前言 现在多数app里面加入聊天已经是 ...

  6. android根据拍摄url获取格式,Android如何通过URI获取文件路径示例代码

    前言 最近在工作的过程中,遇到不同 Android 版本下 URI 采用不同方式来获取文件路径的问题. 因为需求的原因,要求拍照上传或者从相册中选择图片上传,而且图片是需要经过压缩的,大小不能超过2M ...

  7. android 11微信,QQ,支付宝无法调用的问题

    android 11系统升级后,检测手机是否存在微信,支付宝,QQ等三方应用 ,需要在项目的 AndroidManifest.xml 中添加以下代码,否则项目永远检测不出三方应用 <!-- An ...

  8. Android 实现微信,QQ的程序前后台切换:back键切换后台;点击通知栏恢复前台。

    实现类似微信,QQ的前后台切换:back键切换后台:点击通知栏恢复前台. 1.back键切换后台的实现: 这个实现需要在主activity重写onbackpressed()方法.代码如下: @Over ...

  9. Android仿微信,QQ群头像合成

    原文地址:https://www.jb51.net/article/130296.htm 效果图: 作为程序员,首先会评估下工作量吧.在产品眼里,就是把图片合成一起嘛,有啥难度吗?所以工作时间决定了你 ...

最新文章

  1. postman+newman(2)
  2. 【CVPR2020】30篇最新论文抢先看!!!
  3. 报告:AI技术正有效缩小亚洲医疗发展的差距
  4. LGOJ P3919【模板】可持久化数组(可持久化线段树/平衡树)
  5. 2015.11.27---Java
  6. opencv入门_【OpenCV入门之十八】通过形态学操作提取水平与垂直线
  7. Java Bean + 注册验证
  8. 嘉年华ON LINE首次在墨天轮和视频号并机直播,数据库内核技术抢先get
  9. PyTorch 1.0 中文文档:torchvision 参考
  10. 华为Mate30、iPhone11、小米9 Pro 到底怎么选?
  11. Android微信怎么变成黑色,科技知识:安卓微信怎么变成黑色主题 安卓微信变成黑色主题方法...
  12. 【2018.1.14】关于本蒟蒻
  13. 洛谷 P1896 状压DP
  14. C#封装DLL类库,调用类库
  15. 搭建FTP资源服务器
  16. python-matplotlib绘图 -应用subplots_adjust()方法解决图表与画布的间距问题
  17. word2Vec基础背景知识
  18. Keil警告:warning: #223-D: function “xxx“ declared implicitly解决
  19. thinkphp3.1.3框架手册
  20. 刀片计算机学习资料第192篇:6U VPX i7 刀片计算机

热门文章

  1. c#插入日期到mysql数据库
  2. 什么是GAC?它解决了什么问题?
  3. lame mp3 wav文件转mp3 单通道
  4. CG-光栅图形学消隐算法-学习笔记
  5. 二叉树的非递归遍历2
  6. 程序人生 - 学习和分享
  7. All about Div内显示滚动桥
  8. 遭遇网游盗号木马Trojan-PSW.Win32.OnLineGames等
  9. iPad革命:开发者的机遇和挑战
  10. 你专属的斗图聊天神器——aidou for mac