android默认打开方式修改_setcontenttype方法

android默认打开方式修改_setcontenttype方法android打开文件,intent的使用

大家好,又见面了,我是你们的朋友全栈君。如果您正在找激活码,请点击查看最新教程,关注关注公众号 “全栈程序员社区” 获取激活教程,可能之前旧版本教程已经失效.最新Idea2022.1教程亲测有效,一键激活。

Jetbrains全系列IDE稳定放心使用

今天做项目遇到了下载更新APP后自动安装的功能,也就是说当下载之后打开该Apk文件。我们可以通过intent的setDataAndType方法实现,这里列举出更多的打开方式:

我的具体代码实现片段是:

Intent intent = new Intent(Intent.ACTION_VIEW);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); 
intent.setDataAndType(Uri.fromFile(new File(filePath)),"application/vnd.android.package-archive");
startActivity(intent);

filePath是该文件的绝对路径。

uri要换为你要打开的文件绝对路径,类型是”android.net.Uri“ 参考博主:Android 文件打开方式_关中一叶的专栏-CSDN博客_android 打开文件

1.打开所有文件类型

Intent intent = new Intent();    
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);    
intent.setAction(android.content.Intent.ACTION_VIEW);
intent.setDataAndType(uri,"*/*");

2.打开apk文件

Intent intent = new Intent();    
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);    
intent.setAction(android.content.Intent.ACTION_VIEW);    
intent.setDataAndType(uri,"application/vnd.android.package-archive"); 

3.打开Video文件

Intent intent = new Intent("android.intent.action.VIEW");  
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);  
intent.setDataAndType(uri, "video/*");

4.打开audio文件

Intent intent = new Intent("android.intent.action.VIEW");  
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); 
intent.setDataAndType(uri, "audio/*"); 

5.打开HTML文件

Uri uri2 = Uri.parse(uri).buildUpon().encodedAuthority("com.android.htmlfileprovider").scheme("content").encodedPath(param ).build();  
Intent intent = new Intent("android.intent.action.VIEW");  
intent.setDataAndType(uri2, "text/html");

6.打开Image文件

Intent intent = new Intent("android.intent.action.VIEW");  
intent.addCategory("android.intent.category.DEFAULT");  
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); 
intent.setDataAndType(uri, "image/*");

7.打开PPT文件

Intent intent = new Intent("android.intent.action.VIEW");     
intent.addCategory("android.intent.category.DEFAULT");     
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);    
intent.setDataAndType(uri, "application/vnd.ms-powerpoint");

8.打开Excel文件

Intent intent = new Intent("android.intent.action.VIEW");     
intent.addCategory("android.intent.category.DEFAULT");     
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);     
intent.setDataAndType(uri, "application/vnd.ms-excel");

9.打开word文件

Intent intent = new Intent("android.intent.action.VIEW");     
intent.addCategory("android.intent.category.DEFAULT");     
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);intent.setDataAndType(uri, "application/msword");

10.打开CHM文件

Intent intent = new Intent("android.intent.action.VIEW");     
intent.addCategory("android.intent.category.DEFAULT");     
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);intent.setDataAndType(uri, "application/x-chm");

11.打开文本文件

Intent intent = new Intent("android.intent.action.VIEW");     
intent.addCategory("android.intent.category.DEFAULT");     
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
intent.setDataAndType(uri1, "text/plain");

12.打开PDF文件

Intent intent = new Intent("android.intent.action.VIEW");     
intent.addCategory("android.intent.category.DEFAULT");     
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
intent.setDataAndType(uri, "application/pdf");

有关Intent的知识请移步博客:基础总结篇之九:Intent应用详解_LiuHe-CSDN博客

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请联系我们举报,一经查实,本站将立刻删除。

发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/182067.html原文链接:https://javaforall.net

(0)
上一篇 2022年10月14日 下午2:46
下一篇 2022年10月14日 下午2:46


相关推荐

  • 回复有关“清理哲学上的垃圾、雾霾……”的评议

    回复有关“清理哲学上的垃圾、雾霾……”的评议

    2021年12月4日
    46
  • cpu性能天梯图2022「建议收藏」

    cpu性能天梯图2022「建议收藏」下方为排名前30的CPU天梯图,为方便大家查看更多CPU具体型号的排名和评分,请看天梯图后面的图表。cpu新品活动388红包等你抢机会不容错过http://www.adiannao.cn/dnAMDEPYC7763,87,767AMDEPYC7J13,86,006AMDEPYC7713,85,947AMDRyzenThreadripperPRO3995WX,85,365AMDRyzenThreadripper3990X,81,086AMDEPY

    2026年2月1日
    20
  • 越权漏洞(IDOR)测试技巧「建议收藏」

    越权漏洞(IDOR)测试技巧「建议收藏」文章目录一、IDOR介绍二、常见的测试技巧1.改变HTTP请求方法2.路径穿越绕过3.改变Content-type(内容类型)4.用数字ID替换非数字5.大小写替换绕过6.用通配符替换ID7.给Web应用提供一个请求ID,哪怕它没作要求8.HTTP参数污染,为同一参数提供多个值。9.更改文件类型。添加不同的文件扩展名(例如.json,.xml,.config)10.JSON参数污染11.在请求体用数组包装参数值12.尝试不同版本的API三、总结一、IDOR介绍IDOR,InsecureDire.

    2022年6月12日
    62
  • APC 注入

    APC注入APC介绍APC(AsynchronousProcedureCalls,异步过程调用),APC是函数在特定的线程被异步执行。在Windows中APC是一种并发机制,用于异步的IO或

    2021年12月13日
    103
  • CMS收集器和G1收集器的区别「建议收藏」

    CMS收集器和G1收集器的区别「建议收藏」目录CMS收集器和G1收集器的区别区别一:使用范围不一样区别二:STW的时间区别三:垃圾碎片区别四:垃圾回收的过程不一样对于CMS收集器和G1收集器的不同,目前简单写了一下4点,有不足的地方后面再不断的更新修改。CMS收集器和G1收集器的区别 区别一:使用范围不一样  CMS收集器是老年代的收集器,可以配合新生代的Serial和ParNew收集…

    2022年6月3日
    58
  • pycharm2021.11.3激活教程【中文破解版】「建议收藏」

    (pycharm2021.11.3激活教程)好多小伙伴总是说激活码老是失效,太麻烦,关注/收藏全栈君太难教程,2021永久激活的方法等着你。IntelliJ2021最新激活注册码,破解教程可免费永久激活,亲测有效,下面是详细链接哦~https://javaforall.net/100143.html6EK6WKOHUX-eyJsaWNlbnNlSWQi…

    2022年3月28日
    47

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注全栈程序员社区公众号