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)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • Jmeter下载安装配置—测试小白

    Jmeter下载安装配置—测试小白一,进入官网:http://jmeter.apache.org/1.第一步进入官网如下图2.选择进行下载,下载下来为一个压缩包,解压即可。3.我下载的是jmeter4.0版本,对应jdk1.8。然后就进行解压。个人认为要注意3点:1.解压之后压缩包叫apache-jmeter-4.0.zip,如是src.zip后缀的都不对,打开之后会报错不可用,因为里面缺少我们下一步将要配置的环境变量.jar文件…

    2022年5月29日
    34
  • springboot和传统springmvc区别「建议收藏」

    springboot和传统springmvc区别「建议收藏」一、概念1、SpringSpring是一个开源容器框架,可以接管web层,业务层,dao层,持久层的组件,并且可以配置各种bean,和维护bean与bean之间的关系。其核心就是控制反转(IOC),和面向切面(AOP),简单的说就是一个分层的轻量级开源框架。2、SpringMVCSpringMVC属于SpringFrameWork的后续产品,已经融合在SpringWebFlow里面。SpringMVC是一种web层mvc框架,用于替代servlet(处理|响应请求,获取表单参数,表单校

    2022年8月20日
    7
  • 关闭Windows硬盘默认共享「建议收藏」

    在桌面建一个txt文档考入两种方式其中的一种,并将txt文件另存为reg后缀文件。第一种Windows Registry Editor Version 5.00[HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\lanmanserver\parameters] “AutoShareServer”=dword:00000000

    2022年4月16日
    125
  • 3.7v锂电池升压到5v_锂电池升压5伏电路图

    3.7v锂电池升压到5v_锂电池升压5伏电路图1,升压类型,小电流250MA类型2,升压类型,低功耗8uA,600MA类型3,升压类型,升压可达12V,1.2A类型4,升压类型,升压可达24V,1.2A类型5,升压类型,输出5V2.4A类型6,升压类型,输出5V3A类型7,锂电池充电IC,实现边充边放电8,锂电池稳压LDO芯片,和降压芯片1,升压类型,小电流250MA类型PW5410A是一颗低噪声,恒频1.2MHZ的开关电容电压倍增器。PW5410A的输入电压范围2.7V-5V,输出电压5V固

    2022年10月7日
    6
  • 聊聊互联网行业年终奖:大家一起关门哭吧「建议收藏」

    聊聊互联网行业年终奖:大家一起关门哭吧

    2022年2月12日
    56
  • 安卓apk证书_安卓无法打开apk文件

    安卓apk证书_安卓无法打开apk文件获取apk证书文件MD5值

    2022年4月20日
    70

发表回复

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

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